海纳百川

Express API 总结

2020-07-05  本文已影响0人  凛冬已至_123

一、 express.xxx

var express = require("express");
var app = express();

app.use(express.json());
app.post("/xxx", function (req, res) {
  console.log(typeof req.body);
  res.send("成功了");
});

app.listen(4000, function () {
  console.log("Example app listening on port 4000!");
});

这是个中间件,用来将请求体进行JSON转换(如果可以进行JSON转换),并内置了bordy-parse,这样在处理函数中可以用req.body获取请求体,输出为对象

curl localhost:4000/xxx -X POST -d '{"hello": "world"}' --header "Content-Type: application/json"

输出为

object
{ hello: 'world' }
express.static('public')

例如:

curl http://localhost:4000/test.html

访问test.html会自动寻找public目录下的test.html返回

二、app.xxx

app.set('views','lv')

设置模板引擎为ejs

app.set('view engine','ejs')

自定义设置app内置属性

app.set('title', 'My Site')
app.get('title')//'My Site'
app.get('/', function (req, res) {
  res.send('GET request to homepage')
})
app.post('/', function (req, res) {
  res.send('POST request to homepage')
})
var express = require("express");
var app = express();
app.set("views", "lv");
app.set("view engine", "ejs");
app.render('email', function (err, html) {
  // ...
})

app.render('email', { name: 'Tobi' }, function (err, html) {
  // ...
})

app.listen(4000, function () {
  console.log("Example app listening on port 4000!");
});

三、request.xxx

req.get('Content-Type')
// => "text/plain"
// ?name=tobi
req.param('name')
// => "tobi"

// POST name=tobi
req.param('name')
// => "tobi"

// /user/tobi for /user/:name
req.param('name')
// => "tobi"
console.dir(req.ip)
// => '127.0.0.1'
/user/:name
// GET /user/tj
console.dir(req.params.name)
// => 'tj'

四、response.xxx

app.get("/test/:name", function (req, res) {
  res.send([1, 2, 3]);
});
var express = require("express");
var app = express();
app.set("views", "lv");
app.set("view engine", "ejs");
app.use(express.json());
app.use(express.static("public"));
app.get("/test", function (req, res) {
  res.render("index");
});

app.listen(4000, function () {
  console.log("Example app listening on port 4000!");
});
res.status(403).end()
res.status(400).send('Bad Request')
res.status(404).sendFile('/absolute/path/to/404.png')
res.set('Content-Type', 'text/plain')

res.set({
  'Content-Type': 'text/plain',
  'Content-Length': '123',
  'ETag': '12345'
})
res.get('Content-Type')
// => "text/plain"
res.append('Link', ['<http://localhost/>', '<http://localhost:3000/>'])
res.append('Set-Cookie', 'foo=bar; Path=/; HttpOnly')
res.append('Warning', '199 Miscellaneous warning')
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')
  }
})

五、router.xxx 阉割版app-直接上代码体会吧

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

// simple logger for this router's requests
// all requests to this router will first hit this middleware
router.use(function (req, res, next) {
  console.log('%s %s %s', req.method, req.url, req.path)
  next()
})

// this will only be invoked if the path starts with /bar from the mount point
router.use('/bar', function (req, res, next) {
  // ... maybe some additional /bar logging ...
  next()
})

// always invoked
router.use(function (req, res, next) {
  res.send('Hello World')
})

app.use('/foo', router)

app.listen(3000)
router.all('*', requireAuthentication)
router.all('*', loadUser)
router.get('/', function (req, res) {
  res.send('hello world')
})
上一篇下一篇

猜你喜欢

热点阅读