Express framework for Nodejs

Express is a concise and flexible node.js web application framework, providing a series of powerful features to help you create various web applications, and rich HTTP tools. Use Express to quickly build a fully functional website. Express framework core features:

Middleware can be set up to respond to HTTP requests.
Routing tables are defined for performing different HTTP request actions.
HTML pages can be rendered dynamically by passing parameters to the template.

  • 安装 Express npm install express --save
  • routing, the code is as follows
var express = require('express');
var app = express();
 
//  主页输出 "Hello World"
app.get('/', function (req, res) {
   console.log("主页 GET 请求");
   res.send('Hello GET');
})
 
 
//  POST 请求
app.post('/', function (req, res) {
   console.log("主页 POST 请求");
   res.send('Hello POST');
})
 
//  /del_user 页面响应
app.get('/del_user', function (req, res) {
   console.log("/del_user 响应 DELETE 请求");
   res.send('删除页面');
})
 
//  /list_user 页面 GET 请求
app.get('/list_user', function (req, res) {
   console.log("/list_user GET 请求");
   res.send('用户列表页面');
})
 
// 对页面 abcd, abxcd, ab123cd, 等响应 GET 请求
app.get('/ab*cd', function(req, res) {   
   console.log("/ab*cd GET 请求");
   res.send('正则匹配');
})
 
 
var server = app.listen(8081, function () {
 
  var host = server.address().address
  var port = server.address().port
 
  console.log("应用实例,访问地址为 http://%s:%s", host, port)
 
})

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325061387&siteId=291194637