[英]How can I separate an API into diferent files? (routes) Node.js + MySQL
I'm new here.我是新来的。 I wanted to know how can I "separate" mi app.js (API) into several files, mainly for the routes but also the connections too if it is posible.我想知道如何将 mi app.js(API)“分离”成几个文件,主要用于路由,但如果可能的话,也可以用于连接。 I tried several video tutorials but none of them worked, so here is my code (only file), I want the routes in a separated file (routes.js):我尝试了几个视频教程,但都没有奏效,所以这是我的代码(只有文件),我希望将路由放在一个单独的文件(routes.js)中:
const mysql = require('mysql')
const bodyParser = require('body-parser')
const PORT = process.env.PORT || 3050
const app = express()
app.use(bodyParser.json())
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
database: 'shop'
})
app.get('/', (req, res) => {
res.send('Welcome to my API!')
})
//ROUTES!!!
app.get('/customers', (req, res) => {
const sql = 'SELECT * FROM customers'
connection.query(sql, (err, results) => {
if (err) throw err
if (results.length > 0) {
res.json(results)
} else {
res.send('No results!')
}
})
})
app.get('/customers/:id', (req, res) => {
const { id } = req.params
const sql = `SELECT * FROM customers WHERE id = ${id}`
connection.query(sql, (err, result) => {
if (err) throw err
if (result.length > 0) {
res.json(result)
} else {
res.send('No result!')
}
})
})
connection.connect(error => {
if (error) throw error
console.log('Database server running!')
})
app.listen(PORT, () => console.log(`Server running on ${PORT}`))
create n new file ApiRouter.js and add following code in it.
const express = require("express");
const router = express.Router();
router.get('/customers', (req, res) => {
const sql = 'SELECT * FROM customers'
connection.query(sql, (err, results) => {
if (err) throw err
if (results.length > 0) {
res.json(results)
} else {
res.send('No results!')
}
})
})
router.get('/customers/:id', (req, res) => {
const { id } = req.params
const sql = `SELECT * FROM customers WHERE id = ${id}`
connection.query(sql, (err, result) => {
if (err) throw err
if (result.length > 0) {
res.json(result)
} else {
res.send('No result!')
}
})
})
module.exports = router;
//now go to app.js file and add these line of codes below
app.use(bodyParser.json())
const route = require('./path/ApiRouter.js'); // require from file
app.use('/', route);
You can use express.Router object to define some routes in separe file.您可以使用 express.Router 对象在单独的文件中定义一些路由。 Then export the router object and do app.use('/api/some-sub-path', router)
然后导出路由器对象并执行app.use('/api/some-sub-path', router)
You can read more about Router here https://expressjs.com/en/guide/routing.html您可以在此处阅读有关路由器的更多信息https://expressjs.com/en/guide/routing.html
Also I advice you to read this article https://dev.to/santypk4/bulletproof-node-js-project-architecture-4epf另外我建议你阅读这篇文章https://dev.to/santypk4/bulletproof-node-js-project-architecture-4epf
create router in another file by
const router = require('express').Router;
router.get();
and now in app.js use router as
app.use('/' , router);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.