Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
///
import { InversifyExpressServer, Controller, Get, All, Delete, Head, Put, Patch, Post, Method, TYPE } from "inversify-express-utils";
import * as express from "express";
import { Kernel } from "inversify";
let kernel = new Kernel();
module server {
let server = new InversifyExpressServer(kernel);
server
.setConfig((app: express.Application) => {
app.use((req: express.Request, res: express.Response, next: express.NextFunction) => {
console.log("hello world");
next();
});
})
.setErrorConfig((app: express.Application) => {
app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
console.error(err.stack);
res.status(500).send("Something broke!");
});
})
.build()
.listen(3000, "localhost");
import { TYPES } from './Types';
import config from './config';
import AudioSource from './api/services/media_providers/AudioSource';
import AudioFactory from './api/services/media_providers/AudioFactory';
import DiskSource from './api/services/media_providers/disk/DiskSource';
import DiskFactory from './api/services/media_providers/disk/DiskFactory';
let container = new Container();
container.bind(TYPE.Controller).to(Song).whenTargetNamed('Song');
container.bind(TYPES.AudioSource).to(DiskSource);
container.bind(TYPES.AudioFactory).to(DiskFactory);
container.bind(TYPES.Config).toConstantValue(config);
// create server
let server = new InversifyExpressServer(container);
server
.build()
.listen(config.webServer.port, config.webServer.bind_address, () => {
console.log('Now listening on ' + config.webServer.bind_address + ':' + config.webServer.port);
});
if (
!req.header('Content-Type') ||
(req.header('Content-Type') !== 'application/json' && !req.header('Content-Type').includes('application/x-www-form-urlencoded'))
) {
return res.status(406).json({
error: 'Unsupported "Content-Type"'
});
}
return next();
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
let server = new InversifyExpressServer(container, null, null, app);
server.setErrorConfig((app) => {
// 404 handler
app.use((req: Request, res: Response, next: NextFunction) => {
res.status(404).send({
statusCode: 404,
error: 'Route is not found'
});
});
// exceptions handler
app.use((err: Error, req: Request, res: Response, next: NextFunction) => handle(err, req, res, next));
});
export default server.build();
export async function bootstrap(
container: Container,
appPort: number,
dbHost: string,
dbName: string,
...modules: ContainerModule[]
) {
if (container.isBound(TYPES.App) === false) {
const dbClient = await getDatabaseClient(dbHost, dbName);
container.bind(TYPES.DbClient).toConstantValue(dbClient);
container.load(...modules);
// Configure express server
const server = new InversifyExpressServer(container);
server.setConfig((app) => {
// Disable default cache
app.set("etag", false);
// Configure requests body parsing
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Adds some decurity defaults
app.use(helmet());
// Log all requets that hit the server
app.use(reqMiddleware);
constructor() {
let root: string;
// Setup application root
root =
process.env.NODE_ENV === 'development'
? path.normalize(__dirname + '/../..')
: path.normalize('.');
const container = IOCContainer.getInstance().getContainer();
this.server = new InversifyExpressServer(container, undefined, {
rootPath: '/api/v1'
});
this.server.setConfig(app => {
// Add security configuration
secureApp(app);
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE');
res.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept'
);
next();
});
function HttpServer(rootPath, container, moduleMetaKey, module, authProvider) {
this.middlewares = [];
this.viewMiddleware = [];
this.handlers = [function (app) { return app.use(function (err) {
throw new server_exception_1.SugoiServerError(exceptions_constant_1.EXCEPTIONS.GENERAL_SERVER_ERROR.message, exceptions_constant_1.EXCEPTIONS.GENERAL_SERVER_ERROR.code, err);
}); }];
this.httpListeners = new Map();
this._rootPath = rootPath;
this.moduleMetaKey = moduleMetaKey;
this.loadModules(module, container);
this.serverInstance = new inversify_express_utils_1.InversifyExpressServer(container, null, { rootPath: rootPath }, null);
}
Object.defineProperty(HttpServer.prototype, "rootPath", {
protected constructor(rootPath: string,
container: Container,
moduleMetaKey: string,
module: IModuleMetadata,
authProvider: TNewable) {
this._rootPath = rootPath;
this.moduleMetaKey = moduleMetaKey;
this.loadModules(module, container);
this.serverInstance = new InversifyExpressServer(container, null, {rootPath}, null, authProvider);
this.instanceId = StringUtils.generateGuid();
}
import { InversifyExpressServer } from 'inversify-express-utils';
import { iocContainer } from './ioc/ioc.config';
/**
* Create Inversify Express Server.
*/
const server = new InversifyExpressServer(iocContainer, null, { rootPath: '/api' });
export { server };