Skip to content

Instantly share code, notes, and snippets.

@kvasdopil
Forked from tuor4eg/server.js
Last active July 16, 2018 14:40
Show Gist options
  • Save kvasdopil/ae89b9c7bcd3da2f28fe23fac9ac325a to your computer and use it in GitHub Desktop.
Save kvasdopil/ae89b9c7bcd3da2f28fe23fac9ac325a 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 (req, res) => {
await readFileAsync('phonebook.txt');
const count = readBook.toString().split('\n').length - 1;
const str = `Welcome to The Phonebook\nRecords count: ${count}`;
res.write(str);
};
export default (port, callback) => {
const server = http.createServer((req, res) => {
await records(req, res);
res.end(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