当前位置:网站首页>使用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"
}
}
通过浏览器访问后台服务

边栏推荐
- What is concurrent search set? Are you still worried about it? In fact, it is a problem of connected graph, which is not so difficult to understand
- The difference between equals and = =
- EN 45545 R24氧指数测试方法解析
- (1) AAC opening - lifecycle, livedata, ViewModel and source code analysis skills of core component principles (Reprint)
- leetcode - 739. Daily temperature
- Screaming Frog Log File Analyser 中文版安装教程
- 指定环境下安装Jupyter
- Codeworks round 723 (Div. 2)
- @Usage details of postconstruct, initializingbean and initmethod
- 不想项目失控?你需要用对项目管理工具
猜你喜欢

Asynchronous notification mechanism of character device driver

centos随笔03:centos8.2安装mysql

Oracle learning (I)

leetcode - 518. Change II

你所不知道的console

Reference implementation scheme of database database and table division strategy

go for it Easily manage all types of items with "flying items"

(一)aac开篇-核心组件原理之Lifecycle、LiveData、ViewModel与源码分析技巧(转载)

Idea annotation settings

深度学习入门之pytorch安装
随机推荐
js 中 Map 和 Set 的用法及区别
知识图谱入门之---yedda标注
Standardized compilation knowledge
mysql高级特性篇,可以多看看,应付面试
In place reversal of a LinkedList
完整的ES6面试题
Screaming Frog Log File Analyser 中文版安装教程
Supplementary provision plan codeworks round 760 (Div. 3)
uniapp关闭/开启/调节系统声音
【node】npm部分
Sword finger offer 62 The last remaining number in the circle
欧洲家具EN 597-1 跟EN 597-2两个阻燃标准一样吗?
EN 45545-2:2020 T11烟毒性检测
怎么做好项目管理?学会这4个步骤就够了
AS 3744.1标准中提及ISO8191测试,两者测试一样吗?
进程间的通信
指定环境下安装Jupyter
窗帘做EN 1101易燃性测试过程是怎么样的?
Codetop - sort odd ascending even descending linked list
室内膨胀型防火涂料根据BS 476-21 耐火标准测定需要符合几项?