我使用快速生成器创建了一个快速项目。这是我的项目结构
项目结构
users.js
var express = require("express");
var router = express.Router();
router.get("/abc", (req, res) => {
res.json({ connect: "success" });
});
module.exports = router;
所以当我尝试在localhost:3000 / abc上打开它时,我根本得不到任何东西。 它说找不到404
这是我的app.js
var createError = require("http-errors");
var express = require("express");
var path = require("path");
var cookieParser = require("cookie-parser");
var logger = require("morgan");
var indexRouter = require("./routes/index");
var usersRouter = require("./routes/users");
var user = router("./controller/user");
var app = express();
// view engine setup
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "pug");
app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));
app.use("/", indexRouter);
app.use("/users", usersRouter);
app.use("/abc", user);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
//next(createError(404));
res.status(404).send("sorry cant find it");
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};
// render the error page
res.status(err.status || 500);
res.render("error");
});
module.exports = app;
答案 0 :(得分:0)
var express = require("express");
var router = express.Router();
var app = express();
app.get('/abc', function(req, res) {
res.json({ connect: "success" });
});
module.exports = app;
您需要在结尾处导出app
,然后您可以使用app.get
。
或强>
var express = require("express");
var router = express.Router();
router.get("/abc", (req, res) => {
res.json({connect: "success"});
});
module.exports = router;
答案 1 :(得分:0)
module.exports
users.js
路由器
var express = require("express");
var router = express.Router();
router.get("/abc", (req, res) => {
res.json({connect: "success"});
});
module.exports = router;