Thursday 15 June 2017

Express: only 'app.get' routes actually send a response…app.put,post, and delete all get redirected to my 404.

Not really understanding what's going on. Here is my full code:const express = require('express'); const bodyParser = require('body-parser') const path = require('path'); const app = express(); app.use(bodyParser.urlencoded({ extended: false })) app.use(bodyParser.json()) app.use(bodyParser.json({ type: 'application/vnd.api+json' })) //Root API app.get('/api', function (req, res) { res.status(200).send('API is working.'); console.log('API IS WORKING!!!'); }); app.post('/api/post-example', function (req, res) { console.log('About to post...?'); res.send(req.params); }); //This allows the React server to access the Express socket app.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", "http://localhost:3000"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); res.header("Access-Control-Allow-Credentials", true); next(); }); //Redirect for unknown routes app.use(function(req, res) { res.status(404).send('This page dont exsit bro.'); }); module.exports = app; So basically, whenever I change the .post method to .get, everything works fine, but everything else is just not working for some reason. The whole app is exported into an index.js file running on Port 3001

Submitted June 15, 2017 at 04:07PM by tangerto

No comments:

Post a Comment