Skip to content

Commit bb7db3d

Browse files
colincaseyedmorley
andauthoredNov 29, 2024··
Some general improvements to the getting started guide (#347)
* Some general improvements to the getting started guide - Includes a shutdown handler (best practice) - Uses a modern, async-aware test runner (Jest) - Aligns on the change to use port 5006 (+3 squashed commits) * Update README.md Co-authored-by: Ed Morley <[email protected]> * Update package.json Co-authored-by: Ed Morley <[email protected]> * Drop `version` + add `private: true` --------- Co-authored-by: Ed Morley <[email protected]>
1 parent e9c4cd9 commit bb7db3d

File tree

6 files changed

+3758
-1327
lines changed

6 files changed

+3758
-1327
lines changed
 

‎Procfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
web: npm start
1+
web: node index.js

‎README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# node-js-getting-started
22

3-
A barebones Node.js app using [Express 4](http://expressjs.com/).
3+
A barebones Node.js app using [Express](https://expressjs.com/).
44

55
This application supports the [Getting Started on Heroku with Node.js](https://devcenter.heroku.com/articles/getting-started-with-nodejs) article - check it out.
66

@@ -15,7 +15,7 @@ $ npm install
1515
$ npm start
1616
```
1717

18-
Your app should now be running on [localhost:5001](http://localhost:5001/).
18+
Your app should now be running on [localhost:5006](http://localhost:5006/).
1919

2020
## Deploying to Heroku
2121

‎index.js

+24-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
const express = require('express')
22
const path = require('path')
33

4-
const PORT = process.env.PORT || 5001
5-
6-
express()
7-
.use(express.static(path.join(__dirname, 'public')))
8-
.set('views', path.join(__dirname, 'views'))
9-
.set('view engine', 'ejs')
10-
.get('/', (req, res) => res.render('pages/index'))
11-
.listen(PORT, () => console.log(`Listening on ${ PORT }`))
4+
const port = process.env.PORT || 5006
5+
6+
const app = express()
7+
8+
app.use(express.static(path.join(__dirname, 'public')))
9+
app.set('views', path.join(__dirname, 'views'))
10+
app.set('view engine', 'ejs')
11+
12+
app.get('/', (req, res) => {
13+
res.render('pages/index')
14+
})
15+
16+
const server = app.listen(port, () => {
17+
console.log(`Listening on ${port}`)
18+
})
19+
20+
process.on('SIGTERM', async () => {
21+
console.log('SIGTERM signal received: gracefully shutting down')
22+
if (server) {
23+
server.close(() => {
24+
console.log('HTTP server closed')
25+
})
26+
}
27+
})

‎package-lock.json

+3,691-1,283
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
{
22
"name": "node-js-getting-started",
3-
"version": "0.3.0",
4-
"description": "A sample Node.js app using Express 4",
3+
"private": true,
4+
"description": "A sample Node.js app using Express",
55
"engines": {
6-
"node": "22.x"
6+
"node": "18.x || 20.x || 22.x"
77
},
88
"main": "index.js",
99
"scripts": {
1010
"start": "node index.js",
11-
"test": "node test.js"
11+
"test": "jest"
1212
},
1313
"dependencies": {
14-
"ejs": "^3.1.9",
15-
"express": "^4.15.2"
14+
"ejs": "^3.1.10",
15+
"express": "^5.0.1"
1616
},
1717
"devDependencies": {
18-
"tape": "^5.7.4"
18+
"jest": "^29.7.0"
1919
},
2020
"repository": {
2121
"type": "git",

‎test.js

+33-26
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,39 @@
1-
const { spawn } = require('child_process');
1+
const { spawn } = require('child_process')
22
const { request } = require('http')
3-
const { URL } = require('url')
4-
const test = require('tape');
53

6-
// Start the app
7-
const env = Object.assign({}, process.env, {PORT: 5001});
8-
const child = spawn('node', ['index.js'], {env});
4+
const PORT = process.env.PORT || 5006
95

10-
test('responds to requests', (t) => {
11-
t.plan(4);
6+
describe('getting started guide', () => {
7+
let app
128

13-
// Wait until the server is ready
14-
child.stdout.on('data', _ => {
15-
// Make a request to our app
16-
(async () => {
17-
const response = await get('http://127.0.0.1:5001')
18-
// stop the server
19-
child.kill();
20-
// No error
21-
t.false(response.error);
22-
// Successful response
23-
t.equal(response.statusCode, 200);
24-
// Assert content checks
25-
t.notEqual(response.body.indexOf("<title>Node.js Getting Started on Heroku</title>"), -1);
26-
t.notEqual(response.body.indexOf("Getting Started on Heroku with Node.js"), -1);
27-
})();
28-
});
29-
});
9+
beforeEach(async () => {
10+
app = spawn('node', ['index.js'])
11+
app.stdout.on('data', (data) => console.log(data.toString()))
12+
// give the server a short time to start up
13+
return new Promise(resolve => setTimeout(resolve, 500))
14+
})
15+
16+
afterEach(() => {
17+
if (app) {
18+
app.stdout.removeAllListeners()
19+
app.kill('SIGTERM')
20+
}
21+
})
22+
23+
it('should bind to IPv4 and respond to GET /', async () => {
24+
const response = await get(`http://127.0.0.1:${PORT}`)
25+
expect(response.statusCode).toBe(200)
26+
expect(response.body).toMatch("<title>Node.js Getting Started on Heroku</title>")
27+
expect(response.body).toMatch("Getting Started on Heroku with Node.js")
28+
})
29+
30+
it('should bind to IPv6 and respond to GET /', async () => {
31+
const response = await get(`http://[::1]:${PORT}`)
32+
expect(response.statusCode).toBe(200)
33+
expect(response.body).toMatch("<title>Node.js Getting Started on Heroku</title>")
34+
expect(response.body).toMatch("Getting Started on Heroku with Node.js")
35+
})
36+
})
3037

3138
async function get(url) {
3239
return new Promise((resolve, reject) => {
@@ -36,11 +43,11 @@ async function get(url) {
3643
res.on('data', (data) => body += data)
3744
res.on('end', () => {
3845
resolve({
39-
error: false,
4046
statusCode: res.statusCode,
4147
body: body
4248
})
4349
})
50+
res.on('error', reject)
4451
})
4552
req.on('error', reject)
4653
req.end()

0 commit comments

Comments
 (0)
Please sign in to comment.