Skip to content

Instantly share code, notes, and snippets.

@tuor4eg
Last active July 17, 2018 06:35
Show Gist options
  • Save tuor4eg/3510687bb9b3a5c9595d9517f1abb776 to your computer and use it in GitHub Desktop.
Save tuor4eg/3510687bb9b3a5c9595d9517f1abb776 to your computer and use it in GitHub Desktop.
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