我构建了一个node js项目,后来尝试重新安排代码,使文件夹结构符合我的新要求。
旧结构
|-controller
| |--docController.js
|-index.js
字符串
较新的文件结构
|-adapter/express
| |--controller
| |---docController.js
|-route
| |--docRoute.js
|-frameworks
| |--app.js
| |--server.js
型
对于新的结构,我不得不在代码中做一些修改,比如将routes从index.js分离到专用的route.js。将index.js拆分为server和app.js
现在我的服务器正在运行,但每当我击中我的API,它是无法定位路由。
App.js
const docRoutes = require('./../adapters/express/routes/docRoutes');
const app = express();
app.use('/', docRoutes);
// app.all('*', (req, res, next) => {
// next(new AppError(`Can't find ${req.originalUrl} on this server!`, 404));
// });
module.exports = app;
型
server.js
const app = require('./app');
const dotenv = require('dotenv');
dotenv.config({ path: './../config.env' });
const port = process.env.PORT;
const server = app.listen(port, () => {
console.log(`App running on port ${port}...`);
});
process.on('unhandledRejection', err => {
console.log('UNHANDLED REJECTION! 💥 Shutting down...');
console.log(err.name, err.message);
server.close(() => {
process.exit(1);
});
});
型
docController.js
const axios = require('axios');
const FormData = require('form-data');
const dotenv = require('dotenv');
dotenv.config({ path: './../../../config.env' });
SOME CONST VARIABLES FROM dotenv
const fs = require('fs')
function findValueByKey(obj, targetKey) {
SOME BORING CODE
}
exports.getAccessToken = async (req, res, next) => {
SOME BORING CODE
}
FOLLOWED BY FEW EXPORTED FUNCTIONS
型
docRoutes.js
const express = require('express');
const docController = require('./../controllers/docController');
const router = express.Router();
router.get('/get',docController.getAccessToken);
router.post('/create',docController.getAccessToken,docController.createDoc);
router.post('/submit',docController.getAccessToken,docController.addTagsAndSend);
router.get('/preview',docController.getAccessToken,docController.previewDoc);
module.exports = router;
型
代码之前是可以工作的,但是现在它在app和routes之间的某个地方中断了。有经验的Node js的人可以找到这个问题吗?
1条答案
按热度按时间iswrvxsc1#
原来/create没有get处理程序。我只需要在post调用中点击它,它就开始工作了