生产项目中带有节点js错误的路线问题 [英] Problems with my routes in my production project with node js ERROR
问题描述
我在生产中遇到项目问题,我不知道我的配置是否不好或类似的东西,我想知道你是否能看到一些东西,我会留给我一些代码.
I am presenting problems with my project in production, I don't know if I have a bad configuration or something like that, I would like to know if you can see something, I will leave you pieces of my code.
在本地它运行良好,似乎在生产中我使用netlify,但路线并不理想
In local it works perfectly, it seems that in production I use netlify, it does not take the routes well
这是我的路线文件夹中的文件
This is the file in my routes folder
'use strict'
var express = require('express');
var ArticleController = require('../controllers/article');
var router = express.Router();
var multipart = require('connect-multiparty');
var md_upload = multipart({uploadDir: './upload/articles'});
router.get('/', ArticleController.probar);
router.post('/save', ArticleController.save);
router.get('/articles/:last?', ArticleController.getArticles);
router.get('/article/:id', ArticleController.getArticle);
router.put('/article/:id', ArticleController.update);
router.delete('/article/:id', ArticleController.delete);
router.post('/upload-image/:id?', md_upload , ArticleController.upload);
router.get('/get-image/:image', ArticleController.getImage);
router.get('/search/:search', ArticleController.search);
module.exports = router;
所以我转到导出这些路径的位置,这是app.js文件
So I go to where I export those paths, which is the app.js file
'use strict'
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var article_routes = require('./routes/article');
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
// CORS
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Authorization, X-API-KEY, Origin, X-Requested-With,
Content-Type, Accept, Access-Control-Allow-Request-Method');
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, DELETE');
res.header('Allow', 'GET, POST, OPTIONS, PUT, DELETE');
next();
});
app.use('/api', article_routes);
module.exports = app;
然后我进入我的index.js,这是我导入app.js的地方
Then I go to my index.js which is where I import the app.js
'use strict'
var mongoose = require('mongoose');
var port = process.env.PORT || 3900;
var app = require('./app');
mongoose.set('useFindAndModify', false);
mongoose.Promise = global.Promise;
mongoose.connect('mongodb+srv://emocrat3:emocrat3@reactproyecto.plzbz.mongodb.net/reactProyecto?
retryWrites=true&w=majority', { useNewUrlParser: true })
.then(() => {
console.log('La conexion a mongodb se realizo correctamente!');
//Crear servidor y ponerme a escuchar peticiones HTTP
app.listen(port, () => {
console.log('Servidor corriendo en http://localhost:' + port);
});
});
如果您想进一步了解我,或者如果我需要添加一些内容,我将添加我的存储库 Github存储库
I will add my repository if you want to see more in depth or I need something to add Github repository
为了更好地观察错误,我的网站应用网站错误完全在此处显示此处出现错误
And my website to better observe the error App web The error is presented exactly here ERROR HERE
我要打的唯一途径是
router.get ('/ articles /: last?', ArticleController.getArticles);
我进行查询的前端就是这个
And my frontend that I make the query is this
getArticles = () => {
axios.get("http://arthuro-gomez-react.netlify.app/api/articles")
.then(res => {
this.setState({
articles: res.data.articles,
status: 'success'
});
});
}
我希望我已经很好地提出了我的问题,感谢您抽出宝贵时间来研究我的问题.谢谢
I hope I have formulated my question well, I would appreciate your time to take a look at my problem. Thank you
推荐答案
router.get ('/ articles /: last?', ArticleController.getArticles);
尝试删除空格
因为您的路由器中有
router.get('/articles/:last?', ArticleController.getArticles);
您正在做的是
router.get ('/ articles /: last?', ArticleController.getArticles);
并尝试删除,因为我知道您会通过任何参数吗?
And try removing, because I understan that you will pass any param there is it?
: last?
这篇关于生产项目中带有节点js错误的路线问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!