我们有一个简单的json服务器设置,将其作为模块加载。 但是,在发出POST / PUT请求时,不会将数据注入db.json中。
const jsonServer = require("json-server");
const source = require("./db.json");
const repl = require("replify");
const bodyParser = require("body-parser");
const paginationMiddleware = require("./middlewares/pagination");
const filterByAccessRightFor = require("./middlewares/filterByAccessRightFor");
const searchMiddleware = require("./middlewares/search");
const delayMiddleware = require("./middlewares/delay");
const createdAt = require("./middlewares/created_at");
const daterange = require("./middlewares/daterange");
const absence = require("./middlewares/absence");
const bankAccount = require("./middlewares/bankAccount");
const sort = require("./middlewares/sort");
const fileUpload = require("express-fileupload");
const withUser = require("./responseEnhancers/withUser");
const withAbsenceType = require("./responseEnhancers/withAbsenceType");
const withIndexData = require("./responseEnhancers/withIndexData");
const withNationalStateHolidayOverride = require("./responseEnhancers/withNationalStateHolidayOverride");
const withRoleRestrictions = require("./responseEnhancers/withRoleRestrictions");
const compose = require("lodash/fp/compose");
const initSickImagesEndpoint = require("./features/sicks/images/initEndpoint");
const initLoginEndpoint = require("./features/login/initEndpoint");
const path = require("path");
const express = require("express");
const createCalendarEntry = require("./_helpers/createCalendarEntry");
process.on("createCalendarEntry", createCalendarEntry);
const server = jsonServer.create();
const defaultMiddlewares = jsonServer.defaults({
static: path.join(path.resolve(process.cwd()), "server", "files")
});
const router = jsonServer.router(source, {
foreignKeySuffix: "_id"
});
router.render = (req, res) => {
res = compose(
withIndexData(req),
withUser,
withAbsenceType,
withNationalStateHolidayOverride(req),
withRoleRestrictions(req, db)
)(res);
res.jsonp(res.locals.data);
};
const db = router.db;
if (process.env.NODE_ENV === "production")
server.use(express.static(path.join(__dirname, "../build")));
server
.use(
jsonServer.rewriter({
"/de/*": "/$1"
})
)
.use(bodyParser.json())
.use(fileUpload())
.use(defaultMiddlewares)
.use(paginationMiddleware)
.use(searchMiddleware)
.use(delayMiddleware)
.use(createdAt)
.use(daterange)
.use(absence)
.use(bankAccount)
.use(sort);
initLoginEndpoint(server, db);
initSickImagesEndpoint(server, db);
server.route("/sicks").all(filterByAccessRightFor(db));
server.route("/vacations").all(filterByAccessRightFor(db));
server.use(router);
server.listen(3001, "0.0.0.0", () => {
console.log("JSON Server is running");
});
repl("db", server, { db });
exports.db = db;
module.exports = server;
这是我们的index.js,除了db.json没有更新之外,一切都按预期工作。我们有一个脚本正在为数据库设置种子,并且当通过GET访问资源时,将检索正确的数据。 关于那个有什么想法吗?
答案 0 :(得分:1)
您应该提供json文件的路径,而不是使用require
为db.json文件提供JSON对象。
// const source = require("./db.json");
const source = path.join(__dirname, 'db.json')
const router = jsonServer.router(source, {
foreignKeySuffix: "_id"
});