网站首页 > 编程文章 正文
使用到的第三方依赖:
nodemon、body-parse、cookie-parse、ejs、express
package.json文件配置
# 开发环境下加入
"scripts": {
"start": "DEBUG=express:* nodemon index.js"
}
index.js文件
const express = require('express')
const bodyParser = require('body-parser')
const cookParser = require('cookie-parser')
const path = require('path')
const app = express()
const port = 3000
// 解析 使用 req.body 获取到请求参数
app.use(bodyParser.json())
app.use(bodyParser.urlencoded())
// cookie
// 设置 res.cookie('name', 'zhangsan', {maxAge: 10000, httpOnly: true})
// 获取 req.cookies.name
app.use(cookParser())
// 引入路由
// 创建 /routers/index/index.js 文件
const IndexRouter = require('./routers/index/index')
// 定义视图
// 创建 views文件夹
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
// 设置静态文件目录
// 创建 public文件夹
app.use('/public', express.static(path.join(__dirname, 'public')))
// 定义路由
app.use('/', IndexRouter)
// 定义404
app.use((req, res, next) => {
let err = new Error('Not Found')
err.status = 404
next(err)
})
app.use((err, req, res, next) => {
res.status(err.status || 500)
// 这里为了区分请求类型 前端请求内容加入了{type: 'axios'}
// 如果有好的方法可以在评论区回复感谢
if(req.body.type == 'axios'){
res.json({
error: err,
message: err.message
})
}else{
res.render('error', {
title: '系统错误',
error: err,
message: err.message
})
}
})
app.listen(port, () => {
`express-daohang app listen port ${port}!`
})
routers/index/index.js文件
const express = require('express')
const router = express.Router()
router.get('/', (req, res, next) => {
res.cookie('name', 'zhangsan', {maxAge: 10000, httpOnly: true})
res.render('index', {title: 'index'})
})
router.post('/indexAction', (req, res, next) => {
console.log(req.body)
res.json({status: 200, name: req.cookies.name})
})
module.exports = router
view/index/index.ejs文件
// 可以使用cdn <script src="/public/js/axios.min.js"></script>
<script>
axios.post('/indexAction', {
type: 'axios',
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function(response){
console.log(response)
})
.catch(function(error){
console.log(error)
})
</script>
<script src="/public/js/axios.min.js"></script>
<script>
axios.post('/indexAction', {
type: 'axios',
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function(response){
console.log(response)
})
.catch(function(error){
console.log(error)
})
</script>
文件列表
package.json
routers/index/index.js
public/js/axios.min.js
index.js
view
- error.ejs
- index/index.ejs
猜你喜欢
- 2024-09-11 第39章 Django调用验证码(调用验证码接口)
- 2024-09-11 第6章 Vue组件高级(vue组件用法)
- 2024-09-11 【JavaScript】ES6之Promise用法详解及其应用
- 2024-09-11 前后端分离 Vue + Springboot 实现用户列表单页面开发(建议收藏)
- 2024-09-11 在 JS 中如何使用 Ajax 来进行请求
- 2024-09-11 Spring Cloud Alibaba 之 Gateway 服务网关跨域问题
- 2024-09-11 SpringBoot+Vue+Flowable,模拟一个请假审批流程
- 2024-09-11 Vue基础(2)(vue基础笔试题)
- 2024-09-11 vue.js快速入门(vue.js入门教程)
- 2024-09-11 每日优鲜供应链前端团队微前端改造
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- spire.doc (59)
- system.data.oracleclient (61)
- 按键小精灵源码提取 (66)
- pyqt5designer教程 (65)
- 联想刷bios工具 (66)
- c#源码 (64)
- graphics.h头文件 (62)
- mysqldump下载 (66)
- sqljdbc4.jar下载 (56)
- libmp3lame (60)
- maven3.3.9 (63)
- 二调符号库 (57)
- 苹果ios字体下载 (56)
- git.exe下载 (68)
- diskgenius_winpe (72)
- pythoncrc16 (57)
- solidworks宏文件下载 (59)
- qt帮助文档中文版 (73)
- satacontroller (66)
- hgcad (64)
- bootimg.exe (69)
- android-gif-drawable (62)
- axure9元件库免费下载 (57)
- libmysqlclient.so.18 (58)
- springbootdemo (64)
本文暂时没有评论,来添加一个吧(●'◡'●)