Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
174
rated 0 times [  176] [ 2]  / answers: 1 / hits: 19182  / 6 Years ago, mon, february 19, 2018, 12:00:00

I have a RESTful API that I am using postman to make a call to my route /websites. 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.



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. Any amount of help would be great!!
Thank you!


More From » node.js

 Answers
2

The problem is how you are using the app.use and the 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. What you need to do is simply remove the /websites from your routes.



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

});

[#55113] Thursday, February 15, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
albert

Total Points: 652
Total Questions: 105
Total Answers: 108

Location: Pitcairn Islands
Member since Fri, Oct 15, 2021
3 Years ago
;