gpt4 book ai didi

javascript - InMemoryDataService 导致 MongoDB 错误

转载 作者:行者123 更新时间:2023-12-03 02:07:04 24 4
gpt4 key购买 nike

我在后端有一个 MongoDB,在前端有一个 inMemoryDataService,但是每个都会导致另一个崩溃。例子:1. 如果我运行了 inMemoryDataService,我的后​​端注册/登录将无法工作。2. 如果我只运行 mongo,我的 api/emotions 将不会加载。

Any 建议要么两者都工作,要么如何让后端替换 inMemoryDataService。谢谢

内存中数据.service.ts

import { InMemoryDbService } from 'angular-in-memory-web-api';

export class InMemoryDataService implements InMemoryDbService {

createDb() {


const emotions = [
{ id: 11, name: 'HAPPY' },
{ id: 12, name: 'SAD' },
{ id: 13, name: 'STRESSED' },
{ id: 14, name: 'EXCITED' },
{ id: 15, name: 'EMBARRASSED' },
{ id: 16, name: 'SLEEPY' },
{ id: 17, name: 'SURPRISED' },
{ id: 17, name: 'ANXIOUS' },

];

return {emotions};

}
}

server.js

require('rootpath')();
var express = require('express');
var app = express();
var cors = require('cors');
var bodyParser = require('body-parser');
var expressJwt = require('express-jwt');
var path = require('path');
var config = require('config.json');

app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

// use JWT auth to secure the api, the token can be passed in the authorization header or querystring
app.use(expressJwt({
secret: config.secret,
getToken: function (req) {
if (req.headers.authorization && req.headers.authorization.split(' ')[0] === 'Bearer') {
return req.headers.authorization.split(' ')[1];
} else if (req.query && req.query.token) {
return req.query.token;
}
return null;
}
}).unless({ path: ['/users/authenticate', '/users/register'] }));

// routes
app.use('/users', require('./controllers/users.controller'));
app.use('/emotions', require('/controllers/emotion.controller'));

// error handler
app.use(function (err, req, res, next) {
if (err.name === 'UnauthorizedError') {
res.status(401).send('Invalid Token');
} else {
throw err;
}
});

// start server
var port = process.env.NODE_ENV === 'production' ? 80 : 4000;
var server = app.listen(port, function () {
console.log('Server listening on port ' + port);
});

config.json

{
"connectionString": "mongodb://mongodbuser:passsword@danu7.it.nuigalway.ie:2222/mongodbdb",
"apiUrl": "http://localhost:4000",
"secret": "Bearer"
}

最佳答案

找到了答案:将 passThruUnknownUrl: true 添加到 app.module.ts 中的内存数据服务导入中,以便后端数据服务的 http 可以正确传递!

app.module.ts

  imports: [
BrowserModule,
AppRoutingModule,
HttpClientModule,
HttpModule,
FormsModule,

// BootstrapModalModule,
HttpClientInMemoryWebApiModule.forRoot( InMemoryDataService, { passThruUnknownUrl: true } )

],

关于javascript - InMemoryDataService 导致 MongoDB 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49757272/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com