node.js的Express框架

Express框架

[TOC]


基础用法:

const express = require('express');

var server = express();
//请求  '/a.html'
server.use('/a.html', function (req, res) {     //req, res非侵入式
    // res.write();     只能是string
    res.send({a:5,b:0.6});
    res.end();
})

server.use('/b.html', function (req, res) {
    res.send('bbb');
    res.end();
})

server.listen(8080);

三种获取请求的方法

get('/a.html', function (req, res) {})
post('/a.html', function (req, res) {})
use('/a.html', function (req, res) {})

express-static中间件

const express = require('express');
const expressStatic = require('express-static');

var server = express();

server.listen(8080);

//接口
var users = {
    "maid": "123456",
    "maiid": "11111",
    "maiiid": "22222",
};
server.get('/login',function(req,res) {
    var user = req.query['user'];
    var pass = req.query['pass'];

    if(users[user] == null){
        res.send({ok:false,msg:"此用户不存在"});
    } else if (users[user] != pass){
        res.send({ ok: false, msg: "用户名或密码错误" });
    }else{
        res.send({ ok: true, msg: "成功" });
    }
})

//文件
server.use(expressStatic('./www'));

你可能感兴趣的:(node.js的Express框架)