概念
子应用
var app=express() //主应用
var admin=express()//另一个应用
app.use('/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])
const {
response
} = require('express')
const express = require('express')
const app = express()
app.use(express.json()) //处理请求体json
app.use(express.static('yyy')) //静态路径
app.use(express.urlencoded()) //处理body x-www-form-urlencoded
app.use((req, res, next) => {
console.log(req.body)
res.send('hi')
next()
})
app.listen(3000, () => {
console.log('3000')
})
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…])
const {
response
} = require('express')
const express = require('express')
const app = express()
app.set('case sensitive routing', true) //带小写敏感,set要放在前面
app.set('views', 'gouson')
app.set('view engine', 'pug') //pug, ejs ...
app.get('/style.css', (req, res, next) => {
res.send('style.css')
})
app.get('/STYLE.css', (req, res, next) => {
res.send('STYLE.css')
})
app.get('/test', (req, res, next) => {
res.render('test', {
pageTitle: '网页标题' //渲染 传入变量
})
})
app.post('/test', (req, res, next) => {
res.send('post /test')
})
app.put('/test', (req, res, next) => {
res.send('put /test')
})
app.delete('/test', (req, res, next) => {
res.send('delete /test')
})
app.patch('/test', (req, res, next) => {
res.send('patch /test')
})
app.use((req, res, next) => {
console.log(req.body)
res.send('hi')
next()
})
app.listen(3000, () => {
console.log('3000')
})
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])
const express = require('express')
const app = express()
app.set('views', 'gouson')
app.set('view engine', 'pug') //pug, ejs ...
app.get('/users/:id', (req, res, next) => {
console.log(req.params)
console.log(req.query)
res.send('hi')
next()
})
app.listen(3000, () => {
console.log('3000')
})
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)
const express = require('express')
const app = express()
app.set('views', 'gouson')
app.set('view engine', 'pug') //pug, ejs ...
app.get('/test', (req, res, next) => {
// res.set('X-Gouson', 'yes')
// res.append('X-Gouson', 'yes2')
// res.status(401)
// res.send('hi')
// next()
res.format({
'text/plain': function () {
res.send('hey')
},
'text/html': function () {
res.send('<p>hey</p>')
},
'application/json': function () {
res.send({
message: 'hey'
})
},
default: function () {
// log the request and respond with 406
res.status(406).send('Not Acceptable')
}
})
})
app.get('/gouson', (req, res, next) => {
res.send('gouson')
})
app.listen(3000, () => {
console.log('3000')
})
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
const express = require('express')
const app = express()
const user = require('./router/users')
app.use('/users', user)
app.listen(3000, () => {
console.log('3000')
})
users.js
const express = require('express')
var router = express.Router()
router.get('/', (req, res, next) => {
res.send('/users')
})
router.get('/:id', (req, res, next) => {
res.send('/users/:id')
})
router.get('/:id/edit', (req, res, next) => {
res.send('/users/:id/edit')
})
module.exports = router
express generator
$ yarn add global add express-generator
文档