Build express server

express-based Node.js is a minimalist web application development framework.

First, create a file for the application package.js

npm init -y

Add index.js file in the project directory

const express = require('express')
const app = express()

app.get('/', (req, res) => res.send('Hello World!'))

app.listen(3000, () => console.log('Example app listening on port 3000!'))

start up

node index.js

Then your browser to http: // localhost: 3000 / can see the return of 'Hello wORLD!' The text.

Specifies how to route the request URI corresponding response, for example, the following various requests

app.get('/', function (req, res) {
  res.send('Hello World!')
})

app.post('/', function (req, res) {
  res.send('Got a POST request')
})

app.put('/user', function (req, res) {
  res.send('Got a PUT request at /user')
})

app.delete('/user', function (req, res) {
  res.send('Got a DELETE request at /user')
})

Static files

In order to provide CSS files, images, static resources like js file, you can use the built-express.static middleware functions.

express.static(root, [options])

The following code is opening the file in the public directory

app.use(express.static('public'))

Then you can directly access the public directory of files, Express to find files in a static directory, therefore, to store static files directory name does not appear in the URL.

http://localhost:3000/images/kitten.jpg
http://localhost:3000/css/style.css
http://localhost:3000/js/app.js
http://localhost:3000/images/bg.png
http://localhost:3000/hello.html

You can add multiple static resource directory

app.use(express.static('public'))
app.use(express.static('files'))

Can provide a virtual access path, Road King truth does not exist, but when the need to add access.

app.use('/static', express.static('public'))

access

http://localhost:3000/static/images/kitten.jpg
http://localhost:3000/static/css/style.css
http://localhost:3000/static/js/app.js
http://localhost:3000/static/images/bg.png
http://localhost:3000/static/hello.html

It can also provide an absolute path

app.use('/static', express.static(path.join(__dirname, 'public')))

In addition to get, post or the like disposed outside the route, you may also be used express.route () method to create a chain of path routing on the same callback

app.route('/book')
  .get(function (req, res) {
    res.send('Get a random book')
  })
  .post(function (req, res) {
    res.send('Add a book')
  })
  .put(function (req, res) {
    res.send('Update the book')
  })

express.Router

Use express.Router () to create a modular, the router load handler. use can be used as a router load middleware, the following files are birds.js a Router module.

var express = require('express')
var router = express.Router()

// middleware that is specific to this router
router.use(function timeLog (req, res, next) {
  console.log('Time: ', Date.now())
  next()
})
// define the home page route
router.get('/', function (req, res) {
  res.send('Birds home page')
})
// define the about route
router.get('/about', function (req, res) {
  res.send('About birds')
})

module.exports = router

Use as intermediate router module

var birds = require('./birds')

// ...

app.use('/birds', birds)

Then you can use /birds and /birds/about访问了

Guess you like

Origin www.cnblogs.com/ssw-men/p/11490610.html