Node.js builds a simple back-end project, uses POST in express, calls external API, and connects to the database
1. Create a directory and initialize it
mkdir node-server && cd node-server && npm init -y
2. Install express
npm install express --save
3. Install body-parser (use POST in express)
npm install body-parser
4. Install axios (express calls external api)
npm install axios
5. Create a new index.js as
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const router = require('./router')//引用模块
// parse application/x-www-form-urlencoded
//配置模板引擎body-parser一定要在app.use(router)挂载路由之前
app.use(bodyParser.urlencoded({
extended: false}))
// parse application/json
app.use(bodyParser.json())
// 全局 中间件 解决所有路由的 跨域问题
app.all('*', function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'X-Requested-With,Content-Type')
res.header('Access-Control-Allow-Methods', 'GET,POST,PUT')
res.header('Access-Control-Allow-Credentials', true);
res.header("Content-Type", "application/json;charset=utf-8");
next()
})
app.use(router)
app.listen(3033, function () {
console.log("中间服务已启动!监听端口3033")
})
6. Create a new router.js to process the request, and use POST/request external interface in express
//创建路由
const {
Router} = require('express')
const router = new Router()
const axios = require("axios")
const api = 'http://192.168.1.116:8000/api/'
router.post('/inrecognition', (request, response) => {
//处理post请求
let obj = {
deviceId: request.body.deviceId,
deviceName: request.body.deviceName,
}
axios.post(api + 'inrecognition', obj) //请求外部接口
.then((res) => {
response.send(res.data)
})
.catch((error) => {
console.error(error)
response.send(error)
})
})
module.exports = router
7. Start the project
node index.js
Or add startup items in package.json scripts After
adding , the project "start": "node ./index.js"
can be started by npm run start
ornpm start
8. The background of the project startup continues to run, use pm2
to install pm2 globally
npm install pm2 -g
Startup (can also be configured in package.json)
pm2 start index.js
9. Connect to the mysql database
and download the mysql dependencies
npm install mysql
Create a new mysql.js connection database
const mysql = require("mysql")
const Dbs = {
host: 'localhost',
user: 'root',
password: 'root',
database: 'mydb'
}
const connection = mysql.createConnection(Dbs)
connection.connect()
// //查询
// connection.query(`sql`, (err, rows, fields) => {
// if (err) throw err
// console.log(rows)
// })
//
//
// connection.end()
module.exports = connection
Introduce and use in the file that needs to be called, such as in router.js
const mysql = require('./mysql')
transfer
router.get('/getsomething', (req, res) => {
mysql.query(`select * from deviceinfo limit 3`, (err, rows, fields) => {
if (err) throw err
res.send(rows)
})
})
Other Node.js connection database methods refer to https://www.cnblogs.com/joyo-w/p/10660952.html
Expansion: KOAJS https://koa.bootcss.com/