Home > OS >  Getting a 404 error on my express router even though the route is setup and defined
Getting a 404 error on my express router even though the route is setup and defined

Time:03-22

I have built a server for my application to interact with third party API's and be able to access that data from my application in the browser (to get around cors).

I am using express router to create routes for the server which my app can connect to and get the relevant data.

However for the route I have created I am getting a 404 error from client side application and a cannot/GET error on the server url. I am following these docs

code below: server.js

const express = require('express');
const cookieParser = require('cookie-parser');
const passport = require('passport');
const cors = require('cors');
const axios = require('axios');
const mavenlink = require('./routes/router');

const app = express(); 

const corsOptions = {
  origin: '*',
  credentials: true,
  optionSuccessStatus: 200,
};

app.use(express.json());
app.use(cors(corsOptions));
app.use(cookieParser());
app.use(express.urlencoded({ extended: true }));
app.use(passport.initialize());

const port = process.env.PORT || 5000; 


//Server application
app.get('/', (req, res) => {
  res.json("Hello World")

})

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

app.listen(port, () => console.log(`Listening on port ${port}`)); 

routes/router.js

const express = require('express');
const router = express.Router();


router.get('/mavenlink', (req, res) => {
  res.send('Mavenlink connector');
})

module.exports = router;

Can anyone see where I have gone wrong?

CodePudding user response:

your mavenlink route is at GET http://localhost:5000/mavenlink/mavenlink. if you want the moute yo be at http://localhost:5000/mavenlink/ then in your router file write

router.get('/', (req, res) => {
  res.send('Mavenlink connector');
})
  • Related