tristan_california
tristan_california

Reputation: 3

Express endpoint returning 404 for POST but 200 for GET

Built a simple Node app with Express for an API. Sending a GET request works perfectly and I get back a status of 200. However when I send a POST request to the same endpoint i receive a status of 404 Not found.

I've tested with both Postman and cURL and I get the same result with both.

server.js

const express = require("express")
const mongoose = require("mongoose")
const config = require("config")
const axios = require("axios")

const db = config.get("mongoURI")
const port = process.env.PORT || 3000
const app = express()

// Middleware parsing
app.use(express.json())
app.use(express.urlencoded({ extended: false }));

// DATABASE CONNECT
mongoose
  .connect(db, { useNewUrlParser: true })
  .then(() => console.log("Connected to mLab database"))
  .catch(err => console.log("ERROR: ", err))

// ROUTER  
app.use("/api/stocks", require("./routes/api/stocks"))

// POST REQUEST INternal API ##########
function postSymbols() {
  axios.post("http://localhost:3000/api/stocks", "exampleStock")
    .then(res => {
      console.log(res.data.msg)
    })
    .catch(err => console.log("POST postSymbols() ERROR", err.response.status, err.response.statusText))
}

// GET REQUEST INternal API ##########
// CURRENTLY WORKS
function showStocks(){
  axios.get("http://localhost:3000/api/stocks")
    .then(res => console.log(res.data.msg))
    // .then(res => console.log(res.data.stocks))
    .catch(err => console.log("GET showStocks() ERROR", err))
}

// NODE SERVER ##########
app.listen(port, () => {
  console.log("Node server started on: ", port);
  showStocks()
  postSymbols()
})

routes/api/stocks.js

const express = require("express")
const router = express.Router()

const Stock = require("../../model/Stocks")


router.get("/", (req, res) => {
  console.log("GET router hit.")
  Stock.find()
    .then(stocks => res.json({
      stocks,
      msg: "GET request sucessfull."
    }))
})

router.post("/"), (req, res) => {
  console.log("POST router hit.")
  const newStock = new Stock({
    name: req.body.name,
    message: req.body.message,
    date: req.body.date,
    isEnabled: req.body.isEnabled,
    type: req.body.type,
    iexId: req.body.iexId,
    currentPrice: req.body.currentPrice
  })
  newStock.save()
    .then(stocks => res.json({
      stocks,
      msg: "POST request sucessfull!"

    }))
    .catch(err => console.log("POST ERROR: ", err))
}


module.exports = router;

Here are images of the Postman requests and results Postman GET 200 Postman GET 200

Postman POST 404 Postman POST 404

I'm expecting to get res.json success messages for both the GET and POST request, however I'm only getting the res.json success message for GET, and I get a 404 Not Found for the POST request

Upvotes: 0

Views: 2948

Answers (1)

Alan Friedman
Alan Friedman

Reputation: 1610

You have misplaced closing parenthesis on the POST route definition:

router.post("/", (req, res) => {
  ...
});

Upvotes: 2

Related Questions