Gangmax Blog

A node.js Express Example

Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.

Here is an example how to use it. From here and here.

Create basic structure

1
2
3
4
5
mkdir express-sample
cd express-sample
npm init
npm install express
npm install http

Create the source code files

router.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const express = require('express');
const router = express.Router();

// Home page route.
router.get('/', function (req, res) {
res.send('Wiki home page');
})

// About page route.
router.get('/about', function (req, res) {
res.send('About this wiki');
})

module.exports = router;

index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const http = require("http");
const express = require('express');
const router = require('./router');

function createApp() {
let app = express();
app.use('/', router);
return app;
}

let port = 8080;
let app = createApp();
let server = http.createServer(app);
server.listen(port);

Run

1
node index.js

Now you can access “here“ and “here“ to see th result.

And in some cases, if the http server needs some initialization work to do and the initialization returns “promise”, we can change the code as below:

index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
* https://flaviocopes.com/node-http-server/
*
*/
const http = require('http');
const express = require('express');
const router = express.Router();

const port = 3000
const timeout = 5000

console.log('Start initialization...');

var promise1 = new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(timeout);
}, timeout);
});

promise1.then(function(timeout) {
console.log('start server after ' + timeout);

// Home page route.
router.get('/', function (req, res) {
res.send('Wiki home page after timeout: ' + timeout);
});

// About page route.
router.get('/about', function (req, res) {
res.send('About this wiki after timeout: ' + timeout);
});

function createApp() {
const app = express();
app.use('/', router);
return app;
}

const app = createApp();
const server = http.createServer(app);
server.listen(port, () => {
console.log('Server running at http://localhost:%d', port)
});
});

Here is the “async/await” version, which is simpler, I think. From here:

The async function declaration defines an asynchronous function, which returns an AsyncFunction object. An asynchronous function is a function which operates asynchronously via the event loop, using an implicit Promise to return its result. But the syntax and structure of your code using async functions is much more like using standard synchronous functions.

index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
* https://flaviocopes.com/node-http-server/
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
*
*/
const http = require('http');
const express = require('express');
const router = express.Router();


const port = 3000
const timeout = 5000

function resloveAfterGivenTime(timeout) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(timeout);
}, timeout);
});
}

(async function() {
console.log('Start initialization...');

await resloveAfterGivenTime(timeout);

console.log('start server after ' + timeout);

// Home page route.
router.get('/', function (req, res) {
res.send('Wiki home page after timeout: ' + timeout);
});

// About page route.
router.get('/about', function (req, res) {
res.send('About this wiki after timeout: ' + timeout);
});

function createApp() {
const app = express();
app.use('/', router);
return app;
}

const app = createApp();
const server = http.createServer(app);
server.listen(port, () => {
console.log('Server running at http://localhost:%d', port)
});
})();

Comments