GET方法;位置”;node-js应用程序中的路由失败
GET method for a "locations" route in node js application is failing
这是节点js和mongo的第二天。我尝试了各种教程,现在,我正尝试遵循以下教程:https://github.com/amejiarosario/todoAPIjs/commit/d3be6a287e8aff39ab862971da4f050d04e552a1
问题:
我有一个启动的express应用程序,但不会从数据库返回数据。我假设我调用方法不正确,或者在某个地方用大写字母代替了小写字母?或者做了一个应该是复数的单数?如果你能帮我找出我的错误所在,我将不胜感激
代码/背景信息
目前,我只在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 });
路线/位置.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
您缺少服务器创建。
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
之前。
- Emberjs应用程序加载在除Index之外的所有路由上
- 在AngularJS应用程序中使用封装指令和路由的推荐方式是什么
- 什么'在Express中路由时,应用程序级中间件和路由器级中间件之间的区别是什么
- 使用Ampersand.js路由模块化应用程序
- 在通知点击时使用Ion中的OneSignal插件路由到应用程序中的状态
- 如何在不刷新父(应用程序)路由的情况下刷新当前路由
- 在Web应用程序中绑定API和模板路由的位置
- 在单页应用程序上进行客户端路由的正确方法是什么
- 无法在MEAN应用程序中路由到部分视图
- 使用中介pub/sub模式时路由骨干应用程序
- 如何在基于API的多页客户端应用程序中组织路由
- 具有来自静态json文件的路由的主干应用程序(只读)
- 多语言应用程序中的Angular JS路由不起作用
- Slick Carousel在应用程序动态路由的基础上不起作用
- 如何将angular js路由用于node js应用程序
- 如何使用路由提供程序在应用中调用模式服务
- 为什么我的 AngularJS 应用程序在添加路由时不起作用
- 路由在 Angular 和 nodejs 应用程序中不起作用
- Web API 应用程序中 asp.net 路由和路由前缀问题
- Angular Js 和 gapi 用于路由应用程序