-
-
Save kvasdopil/ae89b9c7bcd3da2f28fe23fac9ac325a 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 (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