美文网首页
express入门

express入门

作者: coffee1949 | 来源:发表于2019-06-15 16:32 被阅读0次

基础

let express = require('express');
let app = express();

app.get('/',function(req,res){ 
    res.end('hello,world!')
})
app.get('/admin',function(req,res){ 
    res.end('hello,admin!')
})
app.get('/goods',function(req,res){ 
    res.end('hello,goods!')
})
app.listen(8888)
image.png
image.png
image.png

进阶

// 上面的路由形式:app.get('/',callback)
// 把callback函数抽出成一个单独的文件,如下

// app.js
let express = require('express');
let app = express();
// routes/index.js文件抛出一个方法,这里引入方法并调用且把app当成参数传入
require('./routes/index.js')(app);
app.listen(8888)

// routes/index.js
module.exports = function(app){
    app.get('/',function(req,res){ 
        res.end('hello,world!')
    })
    app.get('/admin',function(req,res){ 
        res.end('hello,admin!')
    })
    app.get('/goods',function(req,res){ 
        res.end('hello,goods!')
    })
}

相关文章

网友评论

      本文标题:express入门

      本文链接:https://www.haomeiwen.com/subject/dfibfctx.html