Home > Blockchain >  Route does not exist on the browser but it works fine on the postman
Route does not exist on the browser but it works fine on the postman

Time:01-06

Okay so i just started building an api using Node. Normally, before i even start, i test it in the postman using dummy data to make sure all the routes are working fine but i never tested it on the browser until today. It brings out the dummy data all fine in the postman but when I put in the same route i used in the postman on the browser tab, it just brings out my custom error message "Route does not exist". Why is this happening?

This is my routes/auth.js

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

const {upload} = require('../utils/multer')
const { register, login } = require('../controllers/auth')

router.post('/register', upload.single('picture'), register)
router.post('/login', login)

module.exports = router

This is my controllers/auth.js:

const register = async (req, res) => {
    res.send('register')
}

const login = async (req, res) => {
    res.send('login')
}

module.exports = {register, login}

This is my app.js:

require('dotenv').config()
require('express-async-errors');

const bodyParser = require('body-parser')
const cors = require('cors')
const multer = require('multer')
const helmet = require('helmet')  //helps you secure your Express apps by setting various HTTP headers.
const morgan = require('morgan')
const path = require('path')


const express = require('express');
const app = express();



/* CONFIGURATIONS */
app.use(helmet());
app.use(helmet.crossOriginResourcePolicy({ policy: "cross-origin" }));
app.use(morgan("common"));
app.use(bodyParser.json({ limit: "30mb", extended: true }));
app.use(express.urlencoded({ limit: "30mb", extended: true }));

app.use("/assets", express.static(path.join(__dirname, "public/assets")));



//routers
const authRouter = require('./routes/auth')



// error handlers
const notFoundMiddleware = require('./middleware/not-found');
const errorHandlerMiddleware = require('./middleware/error-handler');



//middleware
app.use(express.json());
app.use(cors());



//routes
app.use('/api/v1/auth', authRouter)



//errors
app.use(notFoundMiddleware);
app.use(errorHandlerMiddleware);


//database
const connectDB = require('./db/connect');

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

const start = async () => {
  try {
    await connectDB(process.env.MONGO_URI);
    app.listen(port, () =>
      console.log(`Server is listening on port ${port}...`)
    );
  } catch (error) {
    console.log(error);
  }
};

start();

Please note that i do not understand what most of these configurations do, not very well anyways. i have tried to remove them though but the problem was still there.

CodePudding user response:

I am assuming you are trying to access /login or /register route from browser search bar something like this http://host:port/login. With this browser will send GET /login request but /login is a POST method route that is the reason you are getting Route not found

CodePudding user response:

When you send request from your browser then by default it will send GET request and your app is not handling GET requests.

You are handling POST requests for /register and /login routes.

  • Related