gpt4 book ai didi

javascript - 如何以正确的方式拆分 server.js?

转载 作者:行者123 更新时间:2023-12-02 23:18:49 25 4
gpt4 key购买 nike

我正在编写自己的应用程序(后端和前端)。我想问你们我这样做的方式是否正确。我想将 server.js 拆分为几个文件(在 PHP 中我会使用 include() ),但我不确定这是否是正确的方法。

这是一些代码示例:

const app = require('express')(),
fs = require('fs'),
http = require('http').Server(app),
io = require('socket.io')(https),
path = require('path'),
login_user = require('./routes/login_user'),
add_user = require('./routes/add_user'),
logout = require('./routes/logout');


app.post('/login_user', (req, res, next) => {
login_user.go(req, res, next);
});

app.post('/add_user', (req, res) => {
add_user.go(req, res);
});

app.get('/logout', (req, res) => {
logout.go(req, res);
});

请注意,这不是完整的代码,我想专注于将“路由”或“路径”拆分为几个文件。例如,整个 API 登录系统位于/routes/login_user.js 文件(已导出)中。

现在我有这么多路径,代码看起来有点奇怪。

app.post('/check_balance', (req, res) => {
check_balance.go(req, res);
});

app.post('/add_incoming', (req, res) => {
add_incoming.go(req, res);
});

app.post('/add_outgoing', (req, res) => {
add_outgoing.go(req, res);
});

app.post('/add_category', (req, res) => {
add_category.go(req, res);
});

app.post('/change_settings', (req, res) => {
change_settings.go(req, res);
});

app.post('/login_user', (req, res, next) => {
login_user.go(req, res, next);
});

app.post('/add_user', (req, res) => {
add_user.go(req, res);
});

app.post('/verify_user', (req, res) => {
verify_user.go(req, res);
});

app.get('/logout', (req, res) => {
logout.go(req, res);
});

app.get('/check_settings', (req, res) => {
check_settings.go(req, res);
});

app.get('/check_categories', (req, res) => {
check_categories.go(req, res);
});

app.post('/remove_categories', (req, res) => {
remove_categories.go(req, res);
});

app.get('/check_incomings', (req, res) => {
check_incomings.go(req, res);
});

app.get('/check_outgoings', (req, res) => {
check_outgoings.go(req, res);
});

app.get('/check_both', (req, res) => {
check_both.go(req, res);
});

app.get('/check_outgoings_chart', (req, res) => {
check_outgoings_chart.go(req, res);
});

app.get('/check_incomings_chart', (req, res) => {
check_incomings_chart.go(req, res);
});

app.post('/remove_incomings', (req, res) => {
remove_incomings.go(req, res);
});

app.post('/remove_outgoings', (req, res) => {
remove_outgoings.go(req, res);
});

最佳答案

使您的 server.js 尽可能简单,并将所有路由逻辑提取到单独的文件夹(可能将其命名为“routes”)。如果您还想定义您的架构,请将其放在单独的文件夹(“模型”)中。一个完整的解决方案可以是这样的:

在模型文件夹中: 用户.js

const mongoose = require("mongoose"); // In case if you want to use MongoDB
const userSchema = new mongoose.Schema({
name: { type: String, required:true },
email: { type: String, required: true },
password: { type: String, required: true },

});
exports.User = User;

在路线文件夹中:用户.js

const { User } = require("../models/user");
const router = express.Router();

//define your routes here
router.get('/', async(req,res)=>{
const users = await User.find();
res.send(users)
});
module.exports = router;

最后在你的 server.js 中

const app = require('express')(),
fs = require('fs'),
http = require('http').Server(app),
io = require('socket.io')(https),
path = require('path'),
users = require('./routes/users');

app.use("/api/users", users); //With this API Endpoint you can access it like http://{your_domain}/api/users

如果你想让它更干净,你可以将所有路由路径包装到另一个文件夹。我们称之为“启动”。有了这个你就可以这样做。

在您的启动文件夹中:

routes.js

const users = require("../routes/users");

module.exports = function(app) {
app.use("/api/users", users);
//add all routes here
}

然后在你的 server.js 中

require("./startup/routes")(app); //all routes will be imported

关于javascript - 如何以正确的方式拆分 server.js?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57039810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com