简体   繁体   English

在Express中无法开机自检/出错

[英]Getting cannot POST / error in Express

I have a RESTful API that I am using postman to make a call to my route /websites. 我有一个RESTful API,我正在使用邮递员对我的路线/网站进行调用。 Whenever I make the call, postman says "Cannot POST /websites". 每当我打电话时,邮递员都会说“无法张贴/网站”。 I am trying to implement a job queue and I'm using Express, Kue(Redis) and MongoDB. 我正在尝试实现作业队列,并且正在使用Express,Kue(Redis)和MongoDB。

Here is my routes file: 这是我的路线文件:

'use strict';
module.exports = function(app) {
// Create a new website
const websites = require('./controllers/website.controller.js');
app.post('/websites', function(req, res) {
  const content = req.body;
  websites.create(content, (err) => {
    if (err) {
      return res.json({
        error: err,
        success: false,
        message: 'Could not create content',
      });
    } else {
      return res.json({
        error: null,
        success: true,
        message: 'Created a website!', content
      });
    }
  })
});
}

Here is the server file: 这是服务器文件:

const express = require('express');
const bodyParser = require('body-parser');
const kue = require('kue');
const websites = require('./app/routes/website.routes.js')
kue.app.listen(3000);

var app = express();

const redis = require('redis');
const client = redis.createClient();
client.on('connect', () =>{
  console.log('Redis connection established');
})

app.use('/websites', websites);

I've never used Express and I have no idea what is going on here. 我从未使用过Express,也不知道这里发生了什么。 Any amount of help would be great!! 任何数量的帮助将是巨大的! Thank you! 谢谢!

The problem is how you are using the app.use and the app.post. 问题是您如何使用app.use和app.post。 You have. 你有。

app.use('/websites', websites);

And inside websites you have: 在网站内部,您可以:

app.post('/websites', function....

So to reach that code you need to make a post to localhost:3000/websites/websites . 因此,要获取该代码,您需要发布到localhost:3000/websites/websites What you need to do is simply remove the /websites from your routes. 您只需要从路由中删除/websites

//to reach here post to localhost:3000/websites
app.post('/' , function(req, res) {

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM