Katie Kennedy
Katie Kennedy

Reputation: 159

InMemoryDataService causes MongoDB error

I have a MongoDB in the backend and a inMemoryDataService in the front end, but each one causes the other to break. EXAMPLE: 1. If I have the inMemoryDataService running, my back-end registration/login won't work. 2. If I run only the mongo, my api/emotions wont load.

Any suggests to either have both working, or how to have the back-end replacing the inMemoryDataService. Thanks

in-memory-data.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:[email protected]:2222/mongodbdb",
    "apiUrl": "http://localhost:4000",
    "secret": "Bearer"
}

Upvotes: 0

Views: 78

Answers (1)

Katie Kennedy
Katie Kennedy

Reputation: 159

Found the answer: add passThruUnknownUrl: true to your in memory data service import in app.module.ts so the http for backend data services can pass correctly!

app.module.ts

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

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

      ],

Upvotes: 1

Related Questions