Gangmax Blog

自由之思想,独立之精神

A node.js Express Example

| Comments

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
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
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.

Comments