在 Node.JS 中从 API 测试中获取 "Cannot GET/"
Getting "Cannot GET/" from API test in Node.JS
我是 Node.JS 的新手 我正在尝试使用 express API 编译节点但没有成功,尝试调试应用程序只是在第一个模块导入时停止,我从创建类似的应用程序教程运行良好但未保存输入数据,代码如下:
网址:localhost:3000/api/v1/students
Server.js
// Dependences
var bodyParser = require("body-parser");
var express = require("express");
var mongoose = require("mongoose");
var app = express();
//connect to database
mongoose.connect("mongodb://localhost/rest_test");
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.get("/api/v1",require("./routes/api"));
app.listen(3000,
function(req,resp)
{
console.log("is Working bitch!");
});
./routes/api.js
var express = require("express");
var router = express.Router();
var Students = require("../models/Students");
Students.methods(["get","post","put","delete"]);
Students.register(router, "/Students");
module.exports = router;
./models/Students.js
var restful = require("node-restful");
var mongoose= restful.mongoose;
var StudentSchema = new mongoose.Schema(
{
name : String,
course : String
});
module.exports = restful.model("Students",StudentSchema);
已解决,
必须使用:
而不是 get
app.use("/api/v1",require("./routes/api"));
我是 Node.JS 的新手 我正在尝试使用 express API 编译节点但没有成功,尝试调试应用程序只是在第一个模块导入时停止,我从创建类似的应用程序教程运行良好但未保存输入数据,代码如下:
网址:localhost:3000/api/v1/students
Server.js
// Dependences
var bodyParser = require("body-parser");
var express = require("express");
var mongoose = require("mongoose");
var app = express();
//connect to database
mongoose.connect("mongodb://localhost/rest_test");
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.get("/api/v1",require("./routes/api"));
app.listen(3000,
function(req,resp)
{
console.log("is Working bitch!");
});
./routes/api.js
var express = require("express");
var router = express.Router();
var Students = require("../models/Students");
Students.methods(["get","post","put","delete"]);
Students.register(router, "/Students");
module.exports = router;
./models/Students.js
var restful = require("node-restful");
var mongoose= restful.mongoose;
var StudentSchema = new mongoose.Schema(
{
name : String,
course : String
});
module.exports = restful.model("Students",StudentSchema);
已解决,
必须使用:
而不是 getapp.use("/api/v1",require("./routes/api"));