nodejs的web框架express

express中文网

 

引入(先下载)

const express = require('express')

 

使用

const app = express()     //创建服务器

app.get('/',function(req,res){  //get请求
    res.send('你好 express')    //方法都已封装好了,可以不用在单独写响应头
})

app.use('/public/',express.static('./public/'))    //开放公开资源,可在外部通过路径访问public内所有文件

app.get('*', function(req, res) {    //配置404页面,要放在最后,否则静态资源会无法加载
    fs.readFile('./404.html', function(err, data) {
        if (err) {
            res.send('读取错误')
        } else {
            res.setHeader('content-type', 'text/html;charset=utf-8')
            res.send(data)
        }
    })
})

app.listen(8080,function(){    //运行服务器 
  console.log('localhost:8080')
}) 

 

 

 
posted @ 2021-07-05 09:21  终末s  阅读(118)  评论(0)    收藏  举报