Happydevdays Happydevdays - 1 year ago 104
Node.js Question

GET method for a "locations" route in node js application is failing

This is day two for with node js and mongo. I've tried various tutorials and right now, I'm trying to follow this one:


I have an express application that launches, but doesn't return data from my database. I'm assuming that I'm either calling the method incorrectly, or somewhere I've substituted uppercase for lower? Or made something singular that should be plural?
If you can help me pin point where my mistake is, I'd appreciate it.

Code / Background Info

For now, I've only rigged up a GET request in my API.

I launch the application like so:

me@mydevbox:/var/www/html/node/inventory$ DEBUG=inventory ./bin/www
model schema defined
successfully exported database schema
inside locations routing file
connection successful
curl localhost:3000/locations
curl localhost:3000/locations/
curl localhost:3000/Locations/
curl localhost:3000/Locations

I've also tried starting the app by calling "node app.js" and then opening a browser and running "localhost:3000/locations". I get a "localhost refused to connect" error message. However, as you can see from the debug output when I run on via command line, I am connecting to the database... and also creating the schema.

Application Structure:

This is my folder structure for my express application:

me@mydevbox:/var/www/html/node/inventory$ ls -lah
total 44K
drwxr-xr-x 8 jlee jlee 4.0K Aug 2 14:37 .
drwxrwxr-x 4 jlee jlee 4.0K Aug 2 14:22 ..
-rw-rw-r-- 1 jlee jlee 1.8K Aug 2 14:37 app.js
drwxr-xr-x 2 jlee jlee 4.0K Aug 2 14:02 bin
drwxrwxr-x 2 jlee jlee 4.0K Aug 2 14:40 models
drwxrwxr-x 75 jlee jlee 4.0K Aug 2 14:04 node_modules
-rw-rw-r-- 1 jlee jlee 2.9K Aug 2 14:14 npm-debug.log
-rw-rw-r-- 1 jlee jlee 384 Aug 2 14:02 package.json
drwxr-xr-x 5 jlee jlee 4.0K Aug 2 14:02 public
drwxr-xr-x 2 jlee jlee 4.0K Aug 2 14:42 routes
drwxr-xr-x 2 jlee jlee 4.0K Aug 2 14:02 views

The following sections will show you the key pieces of code:


1 var express = require('express');
2 var path = require('path');
3 //var favicon = require('serve-favicon');
4 var logger = require('morgan');
5 var cookieParser = require('cookie-parser');
6 var bodyParser = require('body-parser');
8 var routes = require('./routes/index');
9 var users = require('./routes/users');
10 var locations = require('./routes/locations');
12 var app = express();
14 //mongoose connection info and basic connect test
15 var mongoose = require('mongoose');
16 mongoose.connect('mongodb://', function(err) {
17 if(err) {
18 console.log('connection error', err);
19 } else {
20 console.log('connection successful');
21 }
22 });


var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var loc = require('../models/Location.js');
console.log('inside locations routing file');

/* GET /todos listing. */
router.get('/', function(req, res, next) {
console.log('Inside GET method');
loc.find(function (err, locations) {
if (err) return next(err);

module.exports = router;


var mongoose = require('mongoose');

var LocationSchema = new mongoose.Schema({
sip_domain: String,
brn: String,
ndc: String,
type: String,
playbook_run_date: { type: Date, default: Date.now },
status: Boolean,
tenant_name: String,
customized: Boolean,

console.log('model schema defined');
module.exports = mongoose.model('location', LocationSchema);
console.log('successfully exported database schema');


In the inventory database, I have a collection called "locations" (note it's plural, whereas in my model, I use singular)

I'm currently reading up on express routing here: http://expressjs.com/en/guide/routing.html but in the meantime, if you see where I've gone astray, I'm all ears!



added these lines to bottom of app.js: (right before the module.exports call)

73 var http = require('http');
74 var port = process.env.PORT || 3030;
75 http.createServer(app).listen(port);
77 module.exports = app;

And when I try to test:

me@mydevbox:/var/www/html/node/inventory$ DEBUG=inventory ./bin/www
model schema defined
successfully exported database schema
inside locations routing file
connection successful
curl localhost:3030/locations

Answer Source

You are missing the server creation.

var http = require('http');
var port = process.env.PORT || 8080;

Alternatively, use:

app.listen(port, function () {
  console.log('Listening on port ' + port);

These lines should be at the bottom of app.js, before module.exports.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download