我是软件领域的新手,请耐心回答我的问题和技术术语错误:
前提:- 我已经使用 Angular4 开发了一个前端应用程序。angular 应用程序中定义的 baseURL 是“ http://localhost:3000/ ”。我的应用程序使用 retangular api 与 json-server 交互(我创建了一个名为 json-server 的文件夹,它有 db.json 和 public 文件夹)。当我使用以下命令启动 json-server 时,它运行良好: json-server json-server --watch db.json
我的应用程序已完成,因此我创建了一个生产版本。此后,我将所有文件从 dist 文件夹移动到 json-server 的公共文件夹。当我启动 json-server 时,我的应用程序工作正常。
实际问题:- 现在我想在 azure 中托管。我只是简单地从 json-server 文件夹中复制了所有文件/文件夹(db.json 和 public 文件夹),并将它们放在蔚蓝的云中。当 azure 托管完成并且我在浏览器中打开 url 时,我收到一个错误 - “您无权查看”。
为了纠正上述错误,我从 azure 中删除了所有文件,然后复制了 dist 文件夹中的所有文件并将它们放入 azure cloud 中。使用它,我可以在浏览器中看到应用程序,但看不到图像。在图像上有一个错误 - 响应状态:0 表示 URL:空
当我在本地启动 json-server 时,一切正常,但当然,当从其他机器打开同一个网页时,我得到了同样的错误 - 响应状态:0 表示 URL:空
有什么方法可以在 azure 中运行 json-server 以便所有机器/移动设备在访问 url 时都可以看到正确的网页而不会出现任何错误。
我的 json 服务器上的 id 自动增量有问题。我设置了一个简单的默认 json 服务器,其中包含要观看的单个 db.json 文件。
这是我的 db.json 文件(简单生成的数据):
{
"users": [
{
"id": 2,
"first_name": "Lucy",
"last_name": "Ballmer",
"email": "lucyb56@gmail.com"
},
{
"id": 3,
"first_name": "Anna",
"last_name": "Smith",
"email": "annasmith23@gmail.com"
},
{
"id": 4,
"first_name": "Robert",
"last_name": "Brown",
"email": "bobbrown432@yahoo.com"
},
{
"id": "5",
"first_name": "Roger",
"last_name": "Bacon",
"email": "rogerbacon12@yahoo.com"
},
]
}
Run Code Online (Sandbox Code Playgroud)
现在我尝试使用 Postman 将一些数据发布到 db.json。我已经配置了端点:
http://localhost:4040/users
Run Code Online (Sandbox Code Playgroud)
我正在使用以下数据进行 POST:
{
"first_name": "Marian",
"last_name": "Gowno3333",
"email": "gowno@gmail.com"
}
Run Code Online (Sandbox Code Playgroud)
Json-server 默认情况下会自动递增 ID,但结果我得到了奇怪的 id 格式,如下所示:
"id": "8OZrQkH"
Run Code Online (Sandbox Code Playgroud)
而不是获取例如“id”:6。 …
我使用精彩的json-server作为我的应用程序的后端,它对于访问自定义端点以检索一些数据非常有用.但如果它允许我进行计算/表达以便我也可以模仿后端行为,那将会是非常有用的.
以此数据结构为例
{
"products": [
{
"name": "football",
"id": "SPO-001",
"category": "sport",
"price": 40,
"couponApplied": "false",
"coupons": [
"daw124qdw",
"a1212cxn"
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
我想用某种方式说些什么 "discountPrice": couponApplied ? price * couponDiscount
那只是我的伪编码.但我想做点什么我可以动态计算价格.或者当我提出请求时,它进行计算并返回计算数据(如后端应用程序)
我知道我可以提出请求,申请优惠券并提供新价格.甚至提出要求并更改价格.但这都是客户端完成的.有没有办法用json或json-server或任何其他解决方案来做到这一点.如果这有道理?
我在我的应用程序中多次使用GET并请求应用 RESTful 约定。POST但这会导致我的JSON-server连接中断。有谁知道这背后的原因是什么?知道为什么我会遇到这个问题吗?
我收到的错误是:
POST /points/ 201 10.344 ms - 289
POST /intersections/ 201 15.447 ms - 504
POST /points/ 201 7.159 ms - 307
POST /intersections/ 201 12.089 ms - 486
db.json has changed, reloading...
Loading db.json
db.json has changed, reloading...
Done
Resources
http://localhost:3003/streams
http://localhost:3003/intersections
http://localhost:3003/points
Home
http://localhost:3003
db.json has changed, reloading...
Loading db.json
db.json has changed, reloading...
Done
Resources
http://localhost:3003/streams
http://localhost:3003/intersections
http://localhost:3003/points
Home
http://localhost:3003
Cannot bind to the port 3003. Please specify …Run Code Online (Sandbox Code Playgroud) 我尝试使用json-server如下:
$ json-server --watch db.json
Run Code Online (Sandbox Code Playgroud)
但是,当我运行该命令时,我收到错误或警告,具体取决于我安装的版本:
1.0.0-alpha.1- 1.0.0-alpha.12:
sh: json-server: command not found
Run Code Online (Sandbox Code Playgroud)
或(在 Windows 上):
The term 'json-server' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ json-server --watch db.json
Run Code Online (Sandbox Code Playgroud)
或(如果通过执行npx):
npm ERR! could not determine executable to run
Run Code Online (Sandbox Code Playgroud)
1.0.0-alpha.13:
node:internal/errors:496 …Run Code Online (Sandbox Code Playgroud)假设我们有以下命令行来运行 json-server ( https://github.com/typicode/json-server ):
json-server db.json --routes routes.json --port 8008 --delay 1000
如果我们将 json-server 作为模块运行,我们如何设置这些选项?我可以看到定义的 db.json 和定义的端口。但尚不清楚如何定义其余选项。
const jsonServer = require('json-server');
const server = jsonServer.create();
const router = jsonServer.router('db.json');
const middleWares = jsonServer.defaults();
server.use(middleWares);
router.render = (req, res) => {
console.log(req);
};
server.use(router);
server.listen(8008, () => {
console.log('JSON Server is running');
});
Run Code Online (Sandbox Code Playgroud) 我正在使用 json-server 并且出现以下错误。我做错了什么?
类型错误:无法在 Function.createId (/Users/Picchu/Documents/url/node_modules/json-server/lib/server/mixins.js:47:39) [0] 处读取 Function.createId 处未定义 [0] 的属性“id”[0] .insert (/Users/Picchu/Documents/url/node_modules/lodash-id/src/index.js:47:49) [0] 在 /Users/Picchu/Documents/url/node_modules/lodash/lodash.js:4388 :28 [0] 在 arrayReduce (/Users/Picchu/Documents/url/node_modules/lodash/lodash.js:683:21) [0] 在 baseWrapperValue (/Users/Picchu/Documents/url/node_modules/lodash/lodash. js:4387:14)
createShortUrl(data: ShortUrl): Observable<any> {
let params = new HttpParams();
params = params.append('url', 'http://google.com');
return this._http.post(`${'/api'}`, { params: params }).pipe(map((res) => {
return res;
}
Run Code Online (Sandbox Code Playgroud) 我在 localhost:3000 上使用 json-server。它工作正常,我能够在 angular http.get 中获取数据。
我想阅读X-Total-Countangular .subscribe 中的响应标头。但我无法在响应标头中看到该属性。
但是 chrome 控制台显示 X-Total-Count: 16。
this.catgoryItems.getAllServices(
{
_page: pageIndex || this.pageIndex,
_limit: limit || this.limit
})
.subscribe((response: any) => {
console.log(response);
this.dataSource = response.body;
}, err => {
console.log(err);
this.alertService.showError('Error, plz try again later');
});
Run Code Online (Sandbox Code Playgroud) 我已经在 Windows 中使用 npm 安装了 json-server 。现在,当我尝试从 Windows 命令提示符运行 json-server 命令时,出现以下错误: json-server is not recognized as an internal or external command, operable program or batch file.
有人知道这里需要更改什么吗?
我设置了一个 JSON 文件用作小练习数据库,但我无法运行服务器。
我已经尝试安装(并重新安装)JSON-服务器全球和本地npm install -g json-server和npm install json-server,然后运行json-server --watch db.json,但它不工作。
我还尝试在 package.json 文件中设置一个脚本"load": "json-server --watch db.json"并运行该脚本node load。
但似乎没有任何效果,我不断收到消息:
“未找到‘json-server’命令”
或者,在葡萄牙语中:
“'json-server' não é reconhecido como um comando interno ou externo, um programa operável ou um arquivo em lotes”。
我怎样才能解决这个问题?
json-server ×10
javascript ×4
angular ×3
json ×3
npm ×3
node.js ×2
reactjs ×2
axios ×1
cmd ×1
npm-install ×1
rest ×1
windows ×1