简体   繁体   中英

Express Post Request 404

I'll try to make this as to the point as possible. I am trying to make a post request to my express backend. All of the post requests here work, except for "/addpayment". Here is my file called 'router.js'

module.exports = function(app) {

   app.post('/signin', requireSignin, Authentication.signin)
   app.post('/signup', Authentication.signup)

   app.post('/addpayment', function(req, res, next) {
      res.send({ message: 'why................' })
   })

}

Here is my main 'server.js' file

const express = require('express')
const http = require('http')
const bodyParser = require('body-parser')
const morgan = require('morgan')
const app = express()
const router = require('./router')
const mongoose = require('mongoose')
const cors = require('cors')

// DB Connect
mongoose.connect('mongodb://localhost/demo-app')

// App
app.use(morgan('combined'))
app.use(cors())
app.use(bodyParser.json({ type: '*/*' }))
router(app)

// Server
const port = process.env.PORT || 3090
const server = http.createServer(app)
server.listen(port)
console.log('Server has been started, and is listening on port: ' + port)

I get a 404 in postman, and inside my app browser console. I am using passport in my other routes. I already tried running it through passport when I have a JWT token, and same thing(a 404).

I have already looked at all Stack Overflow/Github posts on the first few pages of google results, with no solution for my use case.

I have made a simplified version of your server and everything works as expected. Only difference that I have made is that I am not creating http server like you, but just calling app.listen

here is working example

router.js

module.exports = function(app) {

  app.post('/addpayment', function(req, res, next) {
    res.send({message: 'why................'})
  })

};

server.js

var express    = require('express');
var router     = require('./router');
var app        = express();

router(app);

//init server
app.listen(3000, function() {
  console.log("Server running on port 3000");
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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