这是节点js和mongo的第二天。我已经尝试了各种教程,现在,我试图遵循这个: https://github.com/amejiarosario/todoAPIjs/commit/d3be6a287e8aff39ab862971da4f050d04e552a1
问题:
我有一个明确的应用程序启动,但不会从我的数据库返回数据。我假设我要么错误地调用方法,要么在某个地方用大写字母替换较低的方法?或者做一些应该是复数的单数? 如果你能帮我指出我的错误,我会很感激。
代码/背景信息
目前,我只在我的API中绑定了一个GET请求 我像这样启动应用程序:
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
我也尝试通过调用" node app.js"来启动应用程序。然后打开浏览器并运行" localhost:3000 / locations"。我得到一个" localhost拒绝连接"错误信息。但是,正如您在通过命令行运行时从调试输出中看到的那样,我正在连接到数据库...并且还创建了模式。
申请结构:
这是我的快递应用程序的文件夹结构:
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
以下部分将向您展示代码的关键部分:
app.js
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');
7
8 var routes = require('./routes/index');
9 var users = require('./routes/users');
10 var locations = require('./routes/locations');
11
12 var app = express();
13
14 //mongoose connection info and basic connect test
15 var mongoose = require('mongoose');
16 mongoose.connect('mongodb://10.66.6.111/inventory', function(err) {
17 if(err) {
18 console.log('connection error', err);
19 } else {
20 console.log('connection successful');
21 }
22 });
路由/ locations.js
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);
res.json(locations);
});
});
module.exports = router;
模型/ Location.js
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');
数据库
在库存数据库中,我有一个名为" locations" (注意它是复数,而在我的模型中,我使用单数)
我目前正在阅读快递路线:http://expressjs.com/en/guide/routing.html但与此同时,如果你看到我误入歧途的地方,我全都听见了!
由于
编辑3
将这些行添加到app.js的底部:(在module.exports调用之前)
73 var http = require('http');
74 var port = process.env.PORT || 3030;
75 http.createServer(app).listen(port);
76
77 module.exports = app;
当我尝试测试时:
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
答案 0 :(得分:0)
您缺少服务器创建。
var http = require('http');
var port = process.env.PORT || 8080;
http.createServer(app).listen(port);
或者,使用:
app.listen(port, function () {
console.log('Listening on port ' + port);
});
这些行应位于app.js
之前module.exports
的底部。