node.js express error: “Cannot GET //v1/foodtruck”

February 17, 2017 1.1k views
Node.js Nginx Ubuntu 16.04

-1
down vote
favorite
I have an app running on node.js. It works locally on my pc but when i upload to my server with digital ocean running on nginx, I keep getting this annoying error message: "Cannot GET //v1/foodtruck".

I have search allover for about two days and still no answer. Other SO that i saw are not related therefore could not resolve my issue hence the reason am asking question.

Can someone please assist me to resolve this issue.

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;

3 Answers

@codeperfect

What does your NGINX server block for your domain look like? If you're proxying with NGINX, there's a few specific settings you need to have within the server block, else it may not work properly.

@jtittle here is my server block code. thanks for the help:

server {
listen 80;

server_name nerdwithatwist.com;

location /api {
    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;
}

}

I just dont hnow why i keep getting this annoying error for over one week now tried everything i could and seem not to be working. error: Cannot GET /api/v1/foodtruck

  • @codeperfect

    The server block looks good to me, so the next question would be are you running a different version of NodeJS locally than what you're running on your Droplet?

    Different versions of NodeJS have different features, some are LTS (Long Term Support), others are what most would call "mainline" which are always the latest, but most likely beta and prone to break from time to time.

    Since you are getting "Cannot GET //v1/foodtruck", NGINX is proxying correctly and the issue it would seem is with Express and how you've defined/setup routing, or an issue between different NodeJS versions and a conflict with Express.

@jtittle am just thinking I should restart the entire configuration all over again. Please advise how I could do a reset to default so I could start configuration all over

  • @codeperfect

    You can start fresh by logging in to the DigitalOcean Control Panel, then clicking on the name of the Droplet you wish to modify. Now click on Destroy from the left side menu and you'll see two options -- Destroy and Rebuild Droplet.

    If you want to keep the same IP, choose Rebuild Droplet and select an OS, then click on the Rebuild button.

Have another answer? Share your knowledge.