当前位置:网站首页>使用express+mysql创建一个基于nodejs的后台服务
使用express+mysql创建一个基于nodejs的后台服务
2022-06-11 08:38:00 【miao_zz】
使用express+mysql创建一个基于nodejs的后台服务
README.md
## 创建一个基于nodejs后端服务的web开发框架
## 项目参考文档
1.nodeJs-->http://nodejs.cn/learn
2.Express-->https://www.expressjs.com.cn/
## 构建项目(开发依赖:--save-dev|-D ; 生产依赖:--save|-S)
1.创建项目文件
mkdir <项目名称> 如:mkdir nodeadmin
2.初始化项目,创建一个package.json
npm init
3.安装Express框架
npm install express --save
4.安装nodemon,随时监听文件的变更,自动重启服务,在package.json中scripts配置,服务启动命令 "dev": "nodemon ./app.js"
npm install nodemon --save-dev
5.安装mysql
npm install mysql -S
项目目录

app.js
const express = require("express");
const app = express();
const host = '127.0.0.1';
const port = 8989;
//用于接收前端发送post请求传过来的数据
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
//请求超时
app.use((req, res, next) => {
req.requestTime = Date.now();
next();
});
//路由管理
const UserRouter = require("./routes/user.js");
app.use('/user', UserRouter);
function error_handle(err, req, res, next) {
if (err) {
res.send({
code: 500,
data: null,
msg: err
})
}
}
app.use(error_handle)
//404异常处理
// 异常处理的注册放在程序的收口处,也就是所有路由的最后,404在express中并不是一个异常,
// 所以没有err对象,用中间件单独处理,当前面路由都没法匹配时,由404中间去处理
function not_found(req, res, next) {
res.send({
code: 404,
data: null,
msg: '路由地址不存在'
})
}
app.use(not_found)
app.listen(port, host, () => {
console.log("[nodejs服务器]:" + host + ':' + port)
})
mysql/index.js
const mysql=require('mysql');
const connection = mysql.createConnection({
host: '127.0.0.1',
user: 'root',
password: '******',
database: 'Resume'
})
connection.connect();
module.exports = connection;
routes/user.js
const express = require('express');
const Router = express.Router();
const DB = require("../mysql/index.js");
Router.get('/', async (req, res) => {
try {
await DB.query('select * from Users', function(err, data) {
if (err) throw new Error(err)
res.send({
code: 200,
data: data,
msg: '请求成功'
})
})
} catch (e) {
//TODO handle the exception
throw new Error(e);
}
})
module.exports = Router;
package.json
{
"name": "nodeadmin",
"version": "1.0.0",
"description": "通过express,mysql构建一个机遇nodejs的后台服务",
"main": "app.js",
"scripts": {
"dev": "nodemon ./src/app.js"
},
"keywords": [
"nodejs",
"express",
"mysql"
],
"author": "zzmiao",
"license": "ISC",
"dependencies": {
"express": "^4.18.1",
"mysql": "^2.18.1"
},
"devDependencies": {
"nodemon": "^2.0.16"
}
}
通过浏览器访问后台服务

边栏推荐
- Interprocess communication
- Timestamp of PostgreSQL and Oracle
- [software tool] the hacker matrix special effect software CMatrix
- [programming development] markdown notes tutorial
- 剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
- Using flying items to manage by objectives, not being a "headless fly" in the workplace
- M1 芯片指南:M1、M1 Pro、M1 Max 和 M1 Ultra
- Web design and website planning assignment 14 add background music to the video
- In place reversal of a LinkedList
- B+超强树,带你知晓MySQL的底层是怎样的结构
猜你喜欢

Oracle learning (I)

Standardized compilation knowledge

补2:圆环回原点问题

M1 芯片指南:M1、M1 Pro、M1 Max 和 M1 Ultra

剑指 Offer 21. 调整数组顺序使奇数位于偶数前面

CentOS essay 03:centos8.2 installing MySQL

leetcode - 518. 零钱兑换 II

结果和目标出入太大?不妨借助目标管理精准直达目标!

Interprocess communication

MySQL advanced features, you can read more about it and meet the interview
随机推荐
Sword finger offer 10- ii Frog jumping on steps
Matlab学习8-图像处理之线性与非线性锐化滤波、非线性平滑滤波
[node] NPM part
Redis cluster in Linux system
Bat batch processing separate environment packaging
Zookepper===> animal management system
GCC AVR(Atmel Studio+ AVR Studio)如何将结构体数组定义在程序存储器(flash)空间并进行读操作
Introduction to database system experiment report answer Experiment 6: advanced query of data table
经典图论,深度优先和广度优先,拓扑,Prim和Krukal,该来温习啦
Pycharm启动卡死,loading project
[programming development] markdown notes tutorial
leetcode - 460. LFU 缓存
Is the result too different from the goal? With the help of target management, you can reach the target accurately!
qiao-lerna:lerna辅助工具
Multiple limit of the same field of SQL
堆是也可以看成一种树结构,规定根节点必须大于或小于左右子节点,但左右子节点的大小顺序没有规定
go for it Easily manage all types of items with "flying items"
Timestamp of PostgreSQL and Oracle
Cyclic sort
Polymorphic interview questions