Last active
July 17, 2018 06:35
-
-
Save tuor4eg/3510687bb9b3a5c9595d9517f1abb776 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const readFileAsync = file => new Promise((resolve, reject) => { | |
fs.readFile(file, (err, data) => err ? reject(err) : resolve(data)); | |
}); | |
const records = async () => { | |
const readBook = await readFileAsync('phonebook.txt'); | |
const count = readBook.toString().split('\n').length - 1; | |
return `Welcome to The Phonebook\nRecords count: ${count}`; | |
}; | |
export default (port, callback) => { | |
const server = http.createServer(async(req, res) => { | |
const message = await records(); | |
res.end(message); | |
}); | |
console.log(callback()); | |
server.listen(port, () => { | |
console.log('Server has been started'); | |
}); | |
}; | |
TEST!!!! | |
import httpAdapter from 'axios/lib/adapters/http'; | |
import server from './solution'; | |
axios.defaults.adapter = httpAdapter; | |
const hostname = 'localhost'; | |
const port = 9000; | |
const url = `http://${hostname}:${port}`; | |
describe('Phonebook', () => { | |
it('GET /', (done) => { | |
server(port, async () => { | |
try { | |
const res = await axios.get(url); | |
expect(res.data).toBe('Welcome to The Phonebook\nRecords count: 1000'); | |
done(); | |
} catch (e) { | |
done.fail(e); | |
} | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment