const express = require( ‘express’ ) //内置模块引用
const app = express()
const port = 8090 //端口
const host = ‘localhost’ //域名
const fs = require( ‘fs’ ) // fs模块读写内容
// express通过路由可以发送信息给前台
app.get(’/home’,( req,res,next ) => {
/*
名词解释:
1. req: request 请求 出发地: 杭州
2. res: response 响应 目的地: 上海
3. next: 表示request到response的一个过程 路程
next决定了请求和响应之间的连通
经验:
req和前台有关 举例: req用来接收前台发来的数据
res和后端有关: 举例: 后端返回给前端的结果
req.query 接收前端发来的get请求携带的数据
*/
// res.send(‘hello’)
console.log( req.query )
fs.writeFile(’./data/user.txt’, JSON.stringify(req.query) , ( error ) => {
console.log( ‘存储成功’ )
})
res.json({
ret: true,
name: ‘Gabriel Yan’
})
})
app.listen( port,host, () => {
console.log( express搭建的服务器运行在:http://${ host }:${ port }
)
})