Author(s): Hayden Smith
Let's look at the iteration 3 spec together.
The hint we'll give you running standups is:
const time = 3; console.log(`Standup start for ${time} seconds!`); function finishStandup() { console.log('Standup is now over'); } setTimeout(finishStandup, time * 1000);
8.1_timeout.tsThere are a few things we want to review for helping you store profile pictures:
import express from 'express'; const app = express(); const port = 3001; app.use('/static', express.static('static')); app.listen(port, () => { console.log(`Listening on port ${port}`); });
8.1_img_serve.tsimport request from 'sync-request'; import fs from 'fs'; const res = request( 'GET', 'http://www.traveller.com.au/content/dam/images/h/1/p/q/1/k/image.related.articleLeadwide.620x349.h1pq27.png/1596176460724.jpg' ); const body = res.getBody(); fs.writeFileSync('test.jpg', body, { flag: 'w' });
8.1_pull_img.tsPlease note: Generally you'll only be able to pull images that are http
(as opposed to https
). Sometimes you can just change the URL and see if it works.
Sending emails is generally considered one of the more challenging parts of the assignment. There a lot of free email services out there. All you need is something that allows you to send emails over SMTP (which you can do via various NodeJS libraries). NodeJS can send requests to an email server to send emails.
If we just throw an exception on the server that isn't caught, it will result in an actual 500
error...
/* eslint-disable no-unreachable */ import express from 'express'; const app = express(); const port = 3001; app.get('/goerror', (req, res) => { throw new Error('Error, oh no!'); res.send('Probably won\'t get this message'); }); app.listen(port, () => { console.log(`Listening on port ${port}`); });
8.1_express_error_1.tsIf we just throw an exception on the server that isn't caught, it will result in an actual 500
error...
/* eslint-disable no-unreachable */ import express from 'express'; const app = express(); const port = 3001; app.get('/goerror', (req, res) => { throw new Error('Error, oh no!'); res.send('Probably won\'t get this message'); }); app.listen(port, () => { console.log(`Listening on port ${port}`); });
8.1_express_error_1.tsThis is bad because it means we can't distinguish between a genuine error on the server, and our server just saying "bad input"
However we can modify this slightly...
/* eslint-disable no-unreachable */ import express from 'express'; import HTTPError from 'http-errors'; import errorHandler from 'middleware-http-errors'; const app = express(); const port = 3001; app.use(express.json()); app.get('/goerror', (req, res) => { throw HTTPError(400, 'Error, oh no!'); res.send('Probably won\'t get this message'); }); app.get('/hello', (req, res) => { res.json({ msg: 'Hello there!' }); }); app.use(errorHandler()); app.listen(port, () => { console.log(`Listening on port ${port}`); });
8.1_express_error_2.tsInstall two libraries and add some middleware.
However we can modify this slightly...
/* eslint-disable no-unreachable */ import express from 'express'; import HTTPError from 'http-errors'; import errorHandler from 'middleware-http-errors'; const app = express(); const port = 3001; app.use(express.json()); app.get('/goerror', (req, res) => { throw HTTPError(400, 'Error, oh no!'); res.send('Probably won\'t get this message'); }); app.get('/hello', (req, res) => { res.json({ msg: 'Hello there!' }); }); app.use(errorHandler()); app.listen(port, () => { console.log(`Listening on port ${port}`); });
8.1_express_error_2.tsInstall two libraries and add some middleware.
Now we need to know how to run coverage with express
and jest
...
Whilst jest
has coverage built in, we need an extra dependency to allow a web server to work with jest. npm install --save-dev nyc
. We also need to update our jest.config.js
(which we've done for you).
We will also set up some useful scripts for us!
Now we just run:
npm run ts-node-coverage theserver.ts
in terminal 1npm run jest thetests.tests.ts
(or just npm test
) in terminal 2coverage/lcov-report/index.html
in the web browser./* eslint-disable no-unreachable */ import express from 'express'; import HTTPError from 'http-errors'; import errorHandler from 'middleware-http-errors'; const app = express(); const port = 3001; app.use(express.json()); app.get('/goerror', (req, res) => { throw HTTPError(400, 'Error, oh no!'); res.send('Probably won\'t get this message'); }); app.get('/hello', (req, res) => { res.json({ msg: 'Hello there!' }); }); app.use(errorHandler()); app.listen(port, () => { console.log(`Listening on port ${port}`); });
8.1_express_error_2.tsimport request from 'sync-request'; describe('Test Apple', () => { test('If it returns a name string successfully', () => { const res = request( 'GET', 'http://localhost:3001/hello', {} ); const bodyObj = JSON.parse(String(res.getBody())); expect(bodyObj.msg).toBe('Hello there!'); }); });
8.1_express_error_2.test.ts