Im currently trying to learn more about Express and Express-Validator but currently facing the following issue: When I'm starting the server and using Postman do excess one of the endpoints the response is not completed. However, it seems like the Validation Chain is being processed but afterwards nothing happens. I have the following modules:
index.ts
import {config} from 'dotenv';
import App from './api/app';
import ControlAmbilight from './api/routes/controlAmbilight';
import AdjustLightning from './app/AdjustLightning';
const ENV_FILE = path.join(__dirname, '..', '.env');
config({path: ENV_FILE});
const PORT = process.env.port || process.env.PORT || 3000;
const ambient = new AdjustLightning();
const app = new App([
new ControlAmbilight(ambient),
], <number> PORT);
app.listen();
app.ts
import * as bodyParser from 'body-parser';
import pino from 'pino';
import expressPino from 'express-pino-logger';
import errorMiddleware from './middleware/errorMiddleware';
export default class App {
private logger: pino.Logger;
private expressLogger;
public app: express.Application;
public port: number;
constructor(controllers: any, port: number) {
this.app = express();
this.port = port;
this.logger = pino({level: process.env.LOG_LEVEL || 'info'});
this.expressLogger = expressPino({logger: this.logger});
this.initializeMiddlewares();
this.initializeControllers(controllers);
this.initializeErrorHandling();
}
private initializeMiddlewares() {
this.app.use(this.expressLogger);
this.app.use(bodyParser.json());
}
private initializeControllers(controllers: any) {
controllers.forEach((controller: any) => {
this.app.use('/', controller.router);
});
}
private initializeErrorHandling() {
this.app.use(errorMiddleware)
}
public listen() {
this.app.listen(this.port, () => {
this.logger.info(`Server running on ${this.port}`);
});
}
}
controlAmbilight.ts
import {Router, Request, Response, NextFunction} from 'express';
import {ValidationChain, check, validationResult} from 'express-validator';
import AdjustLightning from '../../app/AdjustLightning';
// eslint-disable-next-line require-jsdoc
export default class ControlAmbilight {
private ambient: AdjustLightning;
// eslint-disable-next-line new-cap
public router = Router();
public path = '/controlAmbilight';
// eslint-disable-next-line require-jsdoc
constructor(ambient: AdjustLightning) {
this.ambient = ambient;
this.initializeRoutes();
}
// eslint-disable-next-line require-jsdoc
public initializeRoutes() {
this.router.post(this.path, this.controlValidator, this.setAmbilight.bind(this));
}
private controlValidator = (): ValidationChain[] => [
check('on').notEmpty().withMessage('Field \'on\' is required'),
check('on').isBoolean().withMessage('Field \'on\' must be type boolean'),
];
// eslint-disable-next-line require-jsdoc
private setAmbilight(req: Request, res: Response): void {
const errors = validationResult(req);
if (!errors.isEmpty()) {
res.status(422).json({error: errors.array()});
} else {
const isOn = (req.body.on == 'true');
res.send(`The curent state is: ${this.ambient.getIsActive()}`);
}
}
}
I was hopping that someone could explain me what I'm missing here. It seems like I need to call express` next() middleware function, but I'm not sure where to implement it.
EDIT
As requested I'm adding the errorMiddleware:
import { NextFunction, Request, Response } from 'express';
import HttpException from '../exceptions/HttpException';
export default function errorMiddleware (error: HttpException,
request: Request, response: Response, next: NextFunction) {
const status = error.status || 500;
const message = error.message || 'Ups... This did not work :(';
response
.status(status)
.send({ status,
message });
}
And as an additional comment: When I'm adding the Validation Chain directly into the post method within controlAmbilight.ts like that:
public initializeRoutes() {
this.router.post(this.path, [
check('on').notEmpty().withMessage('Field \'on\' is required'),
check('on').isBoolean().withMessage('Field \'on\' must be type boolean'),
], this.setAmbilight.bind(this));
}
It is working as expected.
Related
I'm attempting to use Jest to unit test an express API I've been working on however the database has to be ready before it runs the test. This does not seem to be happening however. I have a server.ts file which contains:
import App from './app';
import UsersController from './controllers/users.controller';
const app = new App();
app.initialize(
[
new UsersController(),
]
);
if (process.env.NODE_ENV !== 'test') {
app.listen();
}
export default app;
app.initialize is an aysnc function which configures the database and routes my controllers.
In my unit test I then have the following
import server from "../server";
import supertest from 'supertest';
const request = supertest(server.app);
it('should allow users to register', async () => {
// Arrange
const user = {
firstName: 'John',
lastName: 'Smith',
age: 42
};
return request.post('/api/users')
.send(user)
.set('Accept', 'application/json')
.then(response => {
expect(response).toEqual(user.firstName)
expect(response.body.lastName).toEqual(user.lastName)
expect(response.body.id).toBeGreaterThan(0)
});
});
This however falls over with a 404 error, however if I remove the NODE_ENV check on "test" in the server file I can see that app.listen() get's called well after my test so I believe it's safe to assume that the tests are running before that file has finished.
For completeness here is my App class:
import "reflect-metadata";
import express from 'express';
import * as bodyParser from 'body-parser';
import {createConnection} from "typeorm";
import IController from './controllers/baseController.interface';
class App {
public app = express();
public port: number = 8080;
public ready: boolean = false;
public async initialize(controllers : [IController]) {
await createConnection().then(async connection => {
connection.synchronize();
this.initializeMiddlewares();
this.initializeControllers(controllers);
});
}
private initializeMiddlewares() {
this.app.use(bodyParser.json());
}
private initializeControllers(controllers : [IController]) {
controllers.forEach((controller) => {
this.app.use('/api/', controller.router);
});
}
public listen() {
this.app.listen(this.port, () => {
console.log(`App listening on the port ${this.port}`);
});
}
}
export default App;
You can try wrapping your App instance creation code inside a function. You can then wait for it inside your tests. In your server.ts do the following:
import App from './app';
import UsersController from './controllers/users.controller';
const getApp = async () => {
const app = new App();
await app.initialize(
[
new UsersController(),
]
);
}
getApp().then( appInstance => {
if (process.env.NODE_ENV !== 'test') {
appInstance.listen();
}})
export default getApp;
In your test file just call the function to get your app instance:
import getApp from "../server";
import supertest from 'supertest';
it('should allow users to register', async () => {
const app = await getApp()
const request = supertest(app);
// Arrange
const user = {
firstName: 'John',
lastName: 'Smith',
age: 42
};
return request.post('/api/users')
.send(user)
.set('Accept', 'application/json')
.then(response => {
expect(response).toEqual(user.firstName)
expect(response.body.lastName).toEqual(user.lastName)
expect(response.body.id).toBeGreaterThan(0)
});
});
I am very new to typescript/javascript, I am trying to build backend rest apis with session
following is app.ts file
import express from "express";
import { applyMiddleware, applyRoutes } from "./utils";
import routes from "./services";
const app = express();
var ses= {
secret: "secret_session",
resave: true,
saveUninitialized: true,
cookie: { maxAge: 3600000,secure: false, httpOnly: true
}
if (app.get('env') === 'production') {
app.set('trust proxy', 1)
ses.cookie.secure = true
}
app.use(session(ses));
applyRoutes(routes, app);
I have started the server and applied the middlewares for error handling but those are not related to question in my opinion so I'm not adding code for it. Following is my routes.ts code where I'm trying to set the session.
import { Request, Response } from "express";
import { getAll, getByKeyword, addNewProduct } from "./productControllers";
{
path: "/api/v1/getAllProducts",
method: "get",
handler: [
(req: Request, res: Response) => {
getAll()
.then((row: any) => {
var sess = req.session;
sess.views = 1;
res.status(200).json({ data: row });
})
.catch(err => {
res.json({
message: err
});
});
}
]
}
I'm getting error at sess.views = 1;
I have tried the suggested questions before asking it, none of them were of any help to me.
EDIT:
I have created an index.ts
import searchRoutes from "./products/routes";
export default [...searchRoutes];
I have another util class
export const applyRoutes = (routes: Route[], router: Router) => {
for (const route of routes) {
const { method, path, handler } = route;
(router as any)[method](path, handler);
}
}
You are using an interface which is Request for express.js. But it doesn't have type definition for session. So typescript throws a compile error. To solve it you need to define session type under Request interface.
You could define a session.d.ts file under your project. And create required types & interfaces. Like:
declare global {
namespace Express {
interface Request {
session?: Session;
sessionID?: string;
}
}
}
interface Session{
mySessionVarible:string
}
But the good thing is we have DefinitilyTyped project which you can find many type definitions. This needs to solve your compile problem.
npm install --save-dev #types/express-session
And don't forget to change your import for Request.
import { Request, Response } from "#types/express-session";
am using angular 6 and express when am developing this api on authentcate uri it returning Http failure response for http://localhost:3000/api/authenticate: 404 Not Found
i have tried removing of the responses on my user.controller.js but the problem persisits it seems am missing out some point here and i dont know here it is at first i got an error saaying cant send headers after they are sent and the error was on my user.controller.js on this line else return res.status(404).json(info);
Here is my user.controller.js
const mongoose = require('mongoose');
const User = mongoose.model('User');
const passport = require('passport');
const _ = require('lodash');
module.exports.register = (req,res, next) => {
const user = new User();
user.fullname = req.body.fullname;
user.email = req.body.email;
user.College = req.body.College;
user.Department = req.body.Department;
user.password = req.body.password;
user.admintype = req.body.admintype;
user.save((err, doc) => {
if(!err) { res.send(doc)}
else
{
if(err.code == 11000)
res.status(422).send(['Duplicate email Address Found.'])
else
return next(err);
}
})
}
module.exports.authenticate = (req, res, next ) => {
//calll for passport authentication
passport.authenticate('local', (err, user, info) => {
//error form paasport middleware
if(err) return res.status(400).json(err);
//registered user
else if (user) return res.status(200).json({ "token":user.generateJwt() });
//unknown user or wrong password
else return res.status(404).json(info);
})(req, res);
}
module.exports.userProfile = (req, res, next) =>{
User.findOne({ _id:req._id},
(err,user) =>{
if(!user)
return res.status(404).json({ status: false, message : 'User Record not Found. '});
else
return res.status(200).json({ status:true , user : _.pick(user, ['fullname','email','university','College','Department','admintype'])});
} );
}
Here is my user.service.ts
```import { Injectable } from '#angular/core';
import { User } from './user.model';
import{ HttpClient, HttpHeaders } from '#angular/common/http';
import{ environment } from '../../environments/environment';
import { from } from 'rxjs';
#Injectable({
providedIn: 'root'
})
export class UserService {
selectedUser: User = {
fullname:'',
email:'',
university:'',
College:'',
Department:'',
password:'',
admintype:''
};
constructor(private http: HttpClient) { }
postUser(user:User)
{
return this.http.post(environment.apiBaseUrl+ '/register' ,user)
}
login(authCredentials)
{
return this.http.post(environment.apiBaseUrl+ '/authenticate',authCredentials);
}
setToken(token:string)
{
localStorage.setItem('token',token);
}
}```
Here is my sign-in.components.ts
```import { Component, OnInit } from '#angular/core';
import { NgForm } from '#angular/forms';
import { UserService } from 'src/app/shared/user.service';
import { Router } from '#angular/router';
#Component({
selector: 'app-sign-in',
templateUrl: './sign-in.component.html',
styleUrls: ['./sign-in.component.css']
})
export class SignInComponent implements OnInit {
constructor( private userService:UserService, private router:Router) { }
model = {
email:'',
password:''
};
emailRegex = /^(([^<>()\[\]\\.,;:\s#"]+(\.[^<>()\[\]\\.,;:\s#"]+)*)|(".+"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
serverErrorMessages : string;
ngOnInit() {
}
onSubmit(form :NgForm)
{
this.userService.login(form.value).subscribe(
res =>{
this.userService.setToken(res['token']);
this.router.navigateByUrl('/signup');
},
err =>{
this.serverErrorMessages = err.message;
});
}
}```
Here is my environment.ts
```/ This file can be replaced during build by using the `fileReplacements` array.
// `ng build --prod` replaces `environment.ts` with `environment.prod.ts`.
// The list of file replacements can be found in `angular.json`.
export const environment = {
production: false,
apiBaseUrl:'http://localhost:3000/api'
};
/*
* For easier debugging in development mode, you can import the following file
* to ignore zone related error stack frames such as `zone.run`, `zoneDelegate.invokeTask`.
*
* This import should be commented out in production mode because it will have a negative impact
* on performance if an error is thrown.
*/
// import 'zone.js/dist/zone-error'; // Included with Angular CLI.```
Here is my auth.js
```const router = require('express').Router();
const User = require('../controller/model/User');
const ctrlUser = require('../controller/user.controller');
const jwthelper = require('../jwtHelper')
//validation
router.post('/register', ctrlUser.register);
router.post('/authenticate',ctrlUser.authenticate);
router.get('/userProfile',jwthelper.verifyJwtToken,ctrlUser.userProfile);
module.exports = router;```
Here is my index.js
```const express = require('express');
const app = express();
const mongoose = require('mongoose');
const dotenv = require('dotenv');
const cors = require('cors')
const bodyParser = require('body-parser');
require('./passportConfig');
const passport = require('passport');
dotenv.config();
//connect to mongodb
mongoose.set('useFindAndModify', false); mongoose.set('useUnifiedTopology', true);
mongoose.connect(process.env.DB_CONNECT,{ useNewUrlParser:true} , () =>
console.log('connected to db!')
);
//import routes
const authRoute = require('./routes/auth');
//middleware
app.use(bodyParser.json());
app.use(cors());
app.use(passport.initialize());
//error handler
app.use((err, req, res, next) =>{
if(err.name =='ValidationError')
{
var valErrs = [];
Object.keys(err.errors).forEach(key => valErrs.push(err.errors[key].message));
res.status(422).send(valErrs);
next();
}
});
//route middleware
app.use('/api',authRoute);
app.listen(3000, () => console.log("server Up and Running"));```
Any Help please on this one please thank you all
The only thing which is remain is to attach the router which you have define in the auth.js file to your express app like this
const authRouter = require('./auth');
And to prefix all routes define in the auth.js file you attach it as a middleware which is trigger on route prifix with \api
const express = require('express');
const app = express();
// define all your middleware and all other route
// and here you attach the auth router
app.use('\api', authRouter);
This will make authentication available on url http://localhost:3000/api/authenticate
You may also get 404 because of this line in authenticate route (by the way I think this must be a 400 - bad request, not 404, which is making confusion.)
else return res.status(404).json(info);
So to understand this, can you replace your authenticate route like this, and see what logs in the api console:
module.exports.authenticate = (req, res, next ) => {
console.log("req.body: ", req.body)
//calll for passport authentication
passport.authenticate('local', (err, user, info) => {
//error form paasport middleware
if(err) return res.status(400).json(err);
//registered user
else if (user) return res.status(200).json({ "token":user.generateJwt() });
//unknown user or wrong password
else {
console.log("info: ", info)
return res.status(400).json(info);
}
})(req, res);
Also it the angular component, can you change your onSubmit like this for easy debug:
be sure your form.value is correct:
onSubmit(form :NgForm)
{
console.log("form.value: ", form.value);
this.userService.login(form.value).subscribe(
res =>{
this.userService.setToken(res['token']);
this.router.navigateByUrl('/signup');
},
err =>{
console.log("err: ", err.message)
this.serverErrorMessages = err.message;
});
}
I have the following express server:
import * as express from "express";
import * as bodyParser from "body-parser";
import * as mongoose from "mongoose";
import { Routes } from "./routes/transactions";
import { authMiddleware } from "./middleware/auth";
import { errorMiddleware } from "./middleware/error";
class App {
public app: express.Application;
public routeProvider: Routes = new Routes();
public mongoUrl: string = "mongodb://...:...#mydomain.com:27017/mycollection"; // This should be read from github CLI
constructor() {
this.app = express();
this.config();
this.routeProvider.routes(this.app);
this.mongoSetup();
}
private config(): void {
this.app.use(bodyParser.json());
this.app.use(bodyParser.urlencoded({ extended: false }));
this.app.use(errorMiddleware);
this.app.use(authMiddleware);
}
private mongoSetup(): void {
mongoose.Promise = global.Promise;
mongoose.connect(
this.mongoUrl,
{ useNewUrlParser: true }
);
}
}
export default new App().app;
This is the authMiddleware (almost the same as the guide on the auth0 site for usage with node.js):
import * as jwt from "express-jwt";
import { expressJwtSecret } from "jwks-rsa";
export function authMiddleware() {
return jwt({
secret: expressJwtSecret({
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 5,
jwksUri: "..."
}),
audience: "...",
issuer: "...",
algorithms: ["RS256"]
});
}
And my error middleware:
import { Request, Response } from "express";
export function errorMiddleware(err, req: Request, res: Response, next) {
console.error(err.stack);
res.status(500).send("Something broke!");
}
Now what would expect to happen - since I broke all the auth and mongodb URLs on purpose, that I would get a status code 500 with the message Something broke!.
Instead I get the
Could not get any response
message when sending a requst using postman.
What am I doing wrong?
How come running this on postman
localhost:3000/api/watson/get-test
it gives error -> TypeError: Cannot read property 'myTest' of undefined
import {Router, Request, Response, NextFunction} from 'express';
export class IbmWatsonRouter {
router: Router;
mytest: any;
/**
* Initialize the Router
*/
constructor() {
this.mytest = new Service();
this.router = Router();
this.init();
}
/**
* Get
*/
public getTest(req: Request, res: Response, next: NextFunction) {
res.send(this.mytest.getSomething());
}
/**
* POST Analyze-Text.
*/
public analyzeText(req: Request, res: Response, next: NextFunction) {
this.mytest.setSomething('aaaa');
res.send('successfully analyze text');
}
/**
* Take each handler, and attach to one of the Express.Router's
* endpoints.
*/
init() {
this.router.get('/get-test', this.getTest);
this.router.post('/analyze-text', this.analyzeText);
}
}
Try to separate router, service and controller. Also, any function in a controller should be static.
Router
import {Router} from 'express';
import {IbmWatsonController} from './controllers/ibmwatson';
export const router = Router();
router.get('/get-test', IbmWatsonController.getTest);
router.post('/analyze-text', IbmWatsonController.analyzeText);
Controller
import {Request, Response, NextFunction} from 'express';
import {Service} from '../services/service';
const serviceInstance = new Service();
export class IbmWatsonController {
public static getTest(req: Request, res: Response, next: NextFunction) {
res.send(serviceInstance.example);
}
public static analyzeText(req: Request, res: Response, next: NextFunction) {
serviceInstance.example = 'aaaa';
res.send('successfully analyze text');
}
}
Service
//#todo: rewrite with stateless solution
export class Service {
privat mytest = 'aaaaa';
get example(): string {
return mytest;
}
set example(val: string): string {
this.mytest = val;
}
}
I believe there is a problem with this line:
this.router.get('/get-test', this.getTest);
this.getTest is an unbound function reference. It has no context.
Try replacing it with:
this.router.get('/get-test', () => this.getTest);
You need to bind the context.
Try with something like this:
this.router.get('/get-test', this.getTest.bind(this));
The complete example:
import {Router, Request, Response, NextFunction} from 'express';
import express from 'express';
class Service {
getSomething() {
return 'Hola';
}
}
class IbmWatsonRouter {
router = null;
mytest = null;
app = null;
/**
* Initialize the Router
*/
constructor() {
this.mytest = new Service();
this.router = Router();
this.init();
}
/**
* Get
*/
getTest(req, res, next) {
//res.send('ok');
res.send(this.mytest.getSomething());
}
/**
* POST Analyze-Text.
*/
analyzeText(req, res, next) {
this.mytest.setSomething('aaaa');
res.send('successfully analyze text');
}
/**
* Take each handler, and attach to one of the Express.Router's
* endpoints.
*/
init() {
this.router.get('/get-test', this.getTest.bind(this));
this.router.post('/analyze-text', this.analyzeText);
this.app = express();
this.app.use(this.router);
this.app.listen(5200, () => {
console.log('App listening on 5200');
});
}
}
const r = new IbmWatsonRouter;