0

I am getting the above error message in my Postman while setting up a new POST request using my heroku url as initial and current values in Globals. Postman returns 'Route does not exist' and when I check the Postman console I get "JSONError: Unexpected token 'R' at 1:1 Route does not exist ^" Postman worked fine up until now I am trying to use heroku with it. The POST request still works fine while the url is local://host/api/v1 ... however with heroku url is gives back that error message. I don't know what I am doing wrong and I have been stuck since last week, help will be appreciated. Thank you

THIS IS MY app.js CODE

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

//extra security packages
const helmet = require('helmet')
const cors = require('cors')
const xss = require('xss-clean')
const rateLimiter = require('express-rate-limit')

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

//connecting dB
const connectDB = require('./db/connect')
const authenticateUser = require('./middleware/authentication')

//import routes
const authRouter = require('./routes/auth')
const jobsRouter = require('./routes/jobs')


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

app.set('trust proxy', 1)

//built in mW
app.use(
  rateLimiter({
    windowMs: 15 * 60 * 1000, //15 minutes
    max: 100, //limit each IP to 100 request per wMs
  })
);
app.use(express.json());
app.use(helmet());
app.use(cors());
app.use(xss());


// routes - full path
app.use('/api/v1/auth', authRouter)
app.use('/api/v1/jobs', authenticateUser, jobsRouter) //authUser is added to this route to prevent unauthorized access to the Jobs route and prevent editing

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

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();

THIS is my auth code

const User = require('../models/User')
const {StatusCodes} = require('http-status-codes')
const { BadRequestError, UnauthenticatedError } = require('../errors')


const register = async (req,res) => {    
    const user = await User.create({...req.body})//'{...req.body}' enables mongoose to do the user validation
    
    const token = user.createJWT()
   
    res
    .status(StatusCodes.CREATED)
    .json({user:{name:user.name}, token})
}

const login = async (req,res) => {
    const {email,password} = req.body
    if(!email || !password){
        throw new BadRequestError('Please provide email and password')
    }
    const user = await User.findOne({email})    
    if(!user){
        throw new UnauthenticatedError('Invalid Credentials')
    }

    const isPasswordCorrect = await user.comparePassword(password)
    if(!isPasswordCorrect){
        throw new UnauthenticatedError('Invalid Credentials')
    }

    const token = user.createJWT()
    res
    .status(StatusCodes.OK)
    .json({user:{name:user.name},token})
}

module.exports = {
    register,
    login,
}

THIS is my routes code

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

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

router.post('/register', register)
router.post('/login', login)

module.exports = router
Palani
  • 21
  • 5
  • Look at the raw response instead of trying to decode it as JSON. What does it say? – ChrisGPT was on strike Aug 01 '22 at 14:09
  • it says 'Route does not exist' – Palani Aug 01 '22 at 14:11
  • So whatever route you're trying to hit doesn't exist. What specific route are you requesting? Is it supposed to be provided by your `authRouter` or your `jobsRouter`? – ChrisGPT was on strike Aug 01 '22 at 14:14
  • I am requesting the Login route. I have 2 globals .. the first with " localhost:5000/api/v1" as the value and it works fine. The second has " https://jobs-api-tutp.herokuapp.com/api/v1" has the value and that is where I have issues. When I send it PM returns "Route does not exist" – Palani Aug 01 '22 at 14:22
  • Okay, well, we can't see the login route here. I guess it's part of your `authRouter`? Please show us the relevant code. And instead of saying "the login route", please show us the actual URL you are requesting. – ChrisGPT was on strike Aug 01 '22 at 14:25
  • @Chris ok done ... I have added login code – Palani Aug 01 '22 at 14:44
  • You still haven't clearly told us what the actual URL you are trying to access is. – ChrisGPT was on strike Aug 01 '22 at 16:32
  • "https://jobs-api-tutp.herokuapp.com/api/v1" ...this is the URL I am trying to access. This is what I have set in my Globals in PM. – Palani Aug 01 '22 at 16:48
  • Okay, but as far as I can see you _don't_ have a route registered there. Shouldn't your login URL be `jobs-api-tutp.herokuapp.com/api/v1/auth/login`? – ChrisGPT was on strike Aug 01 '22 at 16:50
  • yes it should and that's what I have .. "https://jobs-api-tutp.herokuapp.com/api/v1" is set as my Global variable in PM. I am making a new POST request and that is {{PROD_URL}}/auth/login (PROD_URL = https://jobs-api-tutp.herokuapp.com/api/v1) and this gives an error message of route does not exist. However when I use URL = localhost:5000/api/v1 ... it works just fine – Palani Aug 01 '22 at 17:25
  • ...that's not what you said earlier. In any case, something is clearly broken in your routing. I suggest you log or otherwise inspect [the routes that are defined](https://stackoverflow.com/q/14934452/354577) on Heroku. – ChrisGPT was on strike Aug 01 '22 at 17:30
  • I have 2 Global variables in PM. First is URL = localhost:5000/api/v1 and this works fine with POST request {{URL}}/auth/login .... Second is PROD_URL = jobs-api-tutp.herokuapp.com/api/v1 and this gives the error message of 'Route does not exist' when I make POST request {{PROD_URL}}/auth/login – Palani Aug 01 '22 at 17:32
  • @Chris I have also posted my route code in the question – Palani Aug 01 '22 at 17:36
  • I'm not asking about your global variables in Postman, and I'm not asking to see your route code. Heroku says that route doesn't exist, so we need to figure out why. Again, I suggest you log or otherwise inspect the routes that your app has configured on Heroku. – ChrisGPT was on strike Aug 01 '22 at 17:51
  • "Something went wrong while running your scripts. Check Postman Console for more info." ...could it be a problem with my test script const jsonData = pm.response.json() pm.globals.set("accessToken", jsonData.token); – Palani Aug 01 '22 at 18:04
  • @Chris thank you for your support. I finally solved the challenge. – Palani Aug 02 '22 at 14:33

1 Answers1

0

I finally solved the challenge. This is what I did :: I generated a new SSH public key from my computer terminal which I then linked to my heroku settings. I then deleted the old app I had created in my heroku, cleared all existing git repos within my app terminal and quit the app. Restarted my computer and then started the heroku create and deploy app process again from the beginning. Now everything works fine. The problem was not having an SSH linking key. Thank you @Chris for your support.

Palani
  • 21
  • 5