nodejs, Express Cannot GET //v1 error

February 15, 2017 2.7k views
Node.js Nginx MongoDB Ubuntu 16.04

I have an app running on node.js. It works locally on my pc but when i upload to my server with do i keep getting this annoying error message: "Cannot GET //v1/foodtruck"

Can someone please assist me to resolve this issue.

2 Answers

how do you run your NodeJS Express APP? Do you use pm2 ?

  • @baharudinafif yes i use pm2

    Hee is my code. Kindly help me check

    This is my index file in the root of the directory

    index.js

    import http from 'http';
    import bodyparser from 'body-parser';
    import mongoose from 'mongoose';
    import express from 'express';
    import passport from 'passport';
    const LocalStrategy = require('passport-local').Strategy

    import config from './config';
    import routes from './routes';

    let app = express();
    app.server = http.createServer(app);

    //parse appalication/jason
    app.use(bodyparser.json({
    limit: config.bodyLimit
    }));

    //Passport
    app.use(passport.initialize());
    let Account = require('./model/account');
    passport.use(new LocalStrategy({
    usernameField: 'email',
    passwordField: 'password'
    },
    Account.authenticate()
    ));
    passport.serializeUser(Account.serializeUser());
    passport.deserializeUser(Account.deserializeUser());

    //API Routes V1
    app.use('/api/v1', routes);

    app.server.listen(config.port);
    console.log(Started on port ${app.server.address().port});

    export default app;
    this is my controller in my controller directory:

    foodtruck.js

    import mongoose from 'mongoose';
    import { Router } from 'express';
    import FoodTruck from '../model/foodtruck';
    import Review from '../model/review'
    import Report from '../model/report'

    import { authenticate } from '../middleware/authMiddleware'

    export default({ config, db }) => {
    let api = Router();

    // '/V1/restaurant'- read
    api.get('/', (req, res) => {
    FoodTruck.find({}, (err, foodtrucks) => {
    if (err){
    res.send(err)
    }
    res.json(foodtrucks);
    });
    });
    This is routes in routes directory:

    index.js

    import express from 'express';
    import config from '../config';
    import middleware from '../middleware';
    import initializedDb from '../db';
    import foodtruck from '../controller/foodtruck';
    import account from '../controller/account';

    let router = express();

    // Connect to Db

    initializedDb(db => {

    //Internal Middleware
    router.use(middleware({ config, db }));

    // API Routes V1 /V1
    router.use('/foodtruck', foodtruck({ config, db }));
    router.use('/account', account({ config, db }));

    });

    export default router;

The problem will be with your nginx config, although the syntax will pass and the base route will work, for any other route within this, you have to set up your nginx as /v1/, you have to include the extra / after for example,

location /v1 {
proxy_pass http://localhost:3010;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}

Should actually be written as:

location /v1/ {
proxy_pass http://localhost:3010;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}

A slight difference, but it will forward all request to other routes correctly, and wont give the //v1 error, hope this helps you or anyone viewing this.

Have another answer? Share your knowledge.