无法找到合适的 mock-server 来提供具有预定义 url 的 json 响应
Can't find an appropriate mock-server to provide json responses with a predefined url
问题如下:
我正在开发一个前端应用程序(使用 vue.js),我必须寻找一个模拟后端(我不能在前端模拟它)。
在这个应用程序中,我可以发出 http 请求,我只对 GET 和 PATCH 感兴趣,我想向模拟服务器发出这些请求(json:api 兼容,这意味着它 returns 正确的 content-type header 为 application/vnd.api+json).
此服务器必须使用预定义的(已经硬编码的)json 响应进行响应,因此我唯一关心的服务器是它查看请求中的 url 并响应现有 json 文件或 object.
我已经看到了一些解决方案,但没有一个可以配置 url 来接收的解决方案(我只关心如果方法和 url 匹配,我 return 一个 json 文件),例如:
- 请求:(可以是 mysite(点)com 或 localhost:PORTNUMBER)
GET http://localhost:PORTNUMBER/api/projects/1000?include=subtree,subtree.vertices,顶点
然后我用硬编码 json 文件响应
与补丁相同,我实际上并不关心分析 body,只是使用 url 匹配来提供正确的预定义响应,这个没有带来问题,因为url 更简单,我找到的解决方案支持这种类型的 url:
补丁http://localhost:PORTNUMBER/api/projects/500
是否有任何解决方案可以将我的端点配置为 GET 请求的 url?由于 ?include... 部分 url,我发现它们会给我一个不正确的路径错误,因为它们仅作为 PATH/PATH/RESOURCE 使用,但是当它涉及到 PATH/PATH/RESOURCE?any-weird-url-I-need 然后它导致了一个问题,到目前为止我只需要 "as simple" 那样的东西。
我自己用 node.js 尝试了这个:
// app.js, my main file
var express = require("express");
var bodyParser = require("body-parser");
var app = express();
// configuring the body parser to accept JSON as well as url encoded values
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json({
type: ['application/json', 'application/vnd.api+json']
}));
var routes = require("./routes/routes.js")(app);
var server = app.listen(3000, function () {
console.log("Listening on port %s...", server.address().port);
});
在我定义的路由中:
// inside /routes/routes.js
var appRouter = function(app) {
// only works if I let it like "api/projects/1000"
app.get("api/projects/1000?include=subtree,subtree.vertices,vertices", function(req, res) {
res.set('Content-Type', 'application/vnd.api+json');
res.send({-----here goes my json response-----});
});
}
module.exports = appRouter;
设法做到了,然后我使用了
而不是我在 app.get 中使用的东西:
app.get("api/projects/1000", function(req, res) {
res.set('Content-Type', 'application/vnd.api+json');
if(!req.query.include){
res.send(---whatever response when the url doesn't have include---)
return;
}
res.send({-----here goes my json response when it has include-----});
});
也许不是世界上最好的东西,但从这里你可以将它设置为发送以前编写的 json 文件或你需要的任何东西,完全根据你的需要调整,如果有人有建议作为最佳回应, 欢迎评论:)
问题如下:
我正在开发一个前端应用程序(使用 vue.js),我必须寻找一个模拟后端(我不能在前端模拟它)。
在这个应用程序中,我可以发出 http 请求,我只对 GET 和 PATCH 感兴趣,我想向模拟服务器发出这些请求(json:api 兼容,这意味着它 returns 正确的 content-type header 为 application/vnd.api+json).
此服务器必须使用预定义的(已经硬编码的)json 响应进行响应,因此我唯一关心的服务器是它查看请求中的 url 并响应现有 json 文件或 object.
我已经看到了一些解决方案,但没有一个可以配置 url 来接收的解决方案(我只关心如果方法和 url 匹配,我 return 一个 json 文件),例如:
- 请求:(可以是 mysite(点)com 或 localhost:PORTNUMBER)
GET http://localhost:PORTNUMBER/api/projects/1000?include=subtree,subtree.vertices,顶点
然后我用硬编码 json 文件响应
与补丁相同,我实际上并不关心分析 body,只是使用 url 匹配来提供正确的预定义响应,这个没有带来问题,因为url 更简单,我找到的解决方案支持这种类型的 url:
补丁http://localhost:PORTNUMBER/api/projects/500
是否有任何解决方案可以将我的端点配置为 GET 请求的 url?由于 ?include... 部分 url,我发现它们会给我一个不正确的路径错误,因为它们仅作为 PATH/PATH/RESOURCE 使用,但是当它涉及到 PATH/PATH/RESOURCE?any-weird-url-I-need 然后它导致了一个问题,到目前为止我只需要 "as simple" 那样的东西。
我自己用 node.js 尝试了这个:
// app.js, my main file
var express = require("express");
var bodyParser = require("body-parser");
var app = express();
// configuring the body parser to accept JSON as well as url encoded values
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json({
type: ['application/json', 'application/vnd.api+json']
}));
var routes = require("./routes/routes.js")(app);
var server = app.listen(3000, function () {
console.log("Listening on port %s...", server.address().port);
});
在我定义的路由中:
// inside /routes/routes.js
var appRouter = function(app) {
// only works if I let it like "api/projects/1000"
app.get("api/projects/1000?include=subtree,subtree.vertices,vertices", function(req, res) {
res.set('Content-Type', 'application/vnd.api+json');
res.send({-----here goes my json response-----});
});
}
module.exports = appRouter;
设法做到了,然后我使用了
而不是我在 app.get 中使用的东西:app.get("api/projects/1000", function(req, res) {
res.set('Content-Type', 'application/vnd.api+json');
if(!req.query.include){
res.send(---whatever response when the url doesn't have include---)
return;
}
res.send({-----here goes my json response when it has include-----});
});
也许不是世界上最好的东西,但从这里你可以将它设置为发送以前编写的 json 文件或你需要的任何东西,完全根据你的需要调整,如果有人有建议作为最佳回应, 欢迎评论:)