概念

子应用

  1. var app=express() //主应用
  2. var admin=express()//另一个应用
  3. app.use('/admin',admin)//admin作为子应用

挂载点

'/admin'就是admin的挂载点

HTTP

超文本传输协议(Hyper Text Transfer Protocol,HTTP)是一个简单的请求-响应协议,它通常运行在TCP之上。

API

express.xxx 内置中间件

express() express.json([options]) express.raw([options]) express.Router([options]) express.static(root, [options]) express.text([options]) express.urlencoded([options])

  1. const {
  2. response
  3. } = require('express')
  4. const express = require('express')
  5. const app = express()
  6. app.use(express.json()) //处理请求体json
  7. app.use(express.static('yyy')) //静态路径
  8. app.use(express.urlencoded()) //处理body x-www-form-urlencoded
  9. app.use((req, res, next) => {
  10. console.log(req.body)
  11. res.send('hi')
  12. next()
  13. })
  14. app.listen(3000, () => {
  15. console.log('3000')
  16. })

app.xxx 应用设置

app.locals app.mountpath app.on(‘mount’, callback(parent)) app.all(path, callback [, callback …]) app.delete(path, callback [, callback …]) app.disable(name) app.disabled(name) app.enable(name) app.enabled(name) app.engine(ext, callback) app.get(name) app.get(path, callback [, callback …]) app.listen(path, [callback]) app.listen([port[, host[, backlog]]][, callback]) app.METHOD(path, callback [, callback …]) app.param([name], callback) app.path() app.post(path, callback [, callback …]) app.put(path, callback [, callback …]) app.render(view, [locals], callback) app.route(path) app.set(name, value) app.use([path,] callback [, callback…])

  1. const {
  2. response
  3. } = require('express')
  4. const express = require('express')
  5. const app = express()
  6. app.set('case sensitive routing', true) //带小写敏感,set要放在前面
  7. app.set('views', 'gouson')
  8. app.set('view engine', 'pug') //pug, ejs ...
  9. app.get('/style.css', (req, res, next) => {
  10. res.send('style.css')
  11. })
  12. app.get('/STYLE.css', (req, res, next) => {
  13. res.send('STYLE.css')
  14. })
  15. app.get('/test', (req, res, next) => {
  16. res.render('test', {
  17. pageTitle: '网页标题' //渲染 传入变量
  18. })
  19. })
  20. app.post('/test', (req, res, next) => {
  21. res.send('post /test')
  22. })
  23. app.put('/test', (req, res, next) => {
  24. res.send('put /test')
  25. })
  26. app.delete('/test', (req, res, next) => {
  27. res.send('delete /test')
  28. })
  29. app.patch('/test', (req, res, next) => {
  30. res.send('patch /test')
  31. })
  32. app.use((req, res, next) => {
  33. console.log(req.body)
  34. res.send('hi')
  35. next()
  36. })
  37. app.listen(3000, () => {
  38. console.log('3000')
  39. })

req.xxx 操作请求

req.app req.baseUrl req.body req.cookies req.fresh req.hostname req.ip req.ips req.method req.originalUrl req.params req.path req.protocol req.query req.route req.secure req.signedCookies req.stale req.subdomains req.xhr req.accepts(types) req.acceptsCharsets(charset [, …]) req.acceptsEncodings(encoding [, …]) req.acceptsLanguages(lang [, …]) req.get(field) req.is(type) req.param(name [, defaultValue]) req.range(size[, options])

  1. const express = require('express')
  2. const app = express()
  3. app.set('views', 'gouson')
  4. app.set('view engine', 'pug') //pug, ejs ...
  5. app.get('/users/:id', (req, res, next) => {
  6. console.log(req.params)
  7. console.log(req.query)
  8. res.send('hi')
  9. next()
  10. })
  11. app.listen(3000, () => {
  12. console.log('3000')
  13. })

res.xxx 操作响应

res.app res.headersSent res.locals res.append(field [, value]) res.attachment([filename]) res.cookie(name, value [, options]) res.clearCookie(name [, options]) res.download(path [, filename] [, options] [, fn]) res.end([data] [, encoding]) res.format(object) res.get(field) res.json([body]) res.jsonp([body]) res.links(links) res.location(path)
res.redirect([status,] path) res.render(view [, locals] [, callback]) res.send([body]) res.sendFile(path [, options] [, fn]) res.sendStatus(statusCode) res.set(field [, value]) res.status(code) res.type(type) res.vary(field)

  1. const express = require('express')
  2. const app = express()
  3. app.set('views', 'gouson')
  4. app.set('view engine', 'pug') //pug, ejs ...
  5. app.get('/test', (req, res, next) => {
  6. // res.set('X-Gouson', 'yes')
  7. // res.append('X-Gouson', 'yes2')
  8. // res.status(401)
  9. // res.send('hi')
  10. // next()
  11. res.format({
  12. 'text/plain': function () {
  13. res.send('hey')
  14. },
  15. 'text/html': function () {
  16. res.send('<p>hey</p>')
  17. },
  18. 'application/json': function () {
  19. res.send({
  20. message: 'hey'
  21. })
  22. },
  23. default: function () {
  24. // log the request and respond with 406
  25. res.status(406).send('Not Acceptable')
  26. }
  27. })
  28. })
  29. app.get('/gouson', (req, res, next) => {
  30. res.send('gouson')
  31. })
  32. app.listen(3000, () => {
  33. console.log('3000')
  34. })

router.xxx 操作路由

router.all(path, [callback, …] callback) router.METHOD(path, [callback, …] callback) router.param(name, callback) router.route(path) router.use([path], [function, …] function)

app.js

  1. const express = require('express')
  2. const app = express()
  3. const user = require('./router/users')
  4. app.use('/users', user)
  5. app.listen(3000, () => {
  6. console.log('3000')
  7. })

users.js

  1. const express = require('express')
  2. var router = express.Router()
  3. router.get('/', (req, res, next) => {
  4. res.send('/users')
  5. })
  6. router.get('/:id', (req, res, next) => {
  7. res.send('/users/:id')
  8. })
  9. router.get('/:id/edit', (req, res, next) => {
  10. res.send('/users/:id/edit')
  11. })
  12. module.exports = router

express generator

$ yarn add global add express-generator
文档