Skip to content

Commit

Permalink
feat(cors): add cors relsoves #6
Browse files Browse the repository at this point in the history
  • Loading branch information
wzr1337 committed Feb 7, 2017
1 parent 881765e commit ea5208a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ jspm_packages
.npm

# Optional REPL history
.node_repl_history
.node_repl_history

# Mac stuff
.DS_Store
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@types/winston": "^2.2.0",
"body-parser": "^1.15.2",
"command-line-args": "^3.0.5",
"cors": "^2.8.1",
"express": "^4.14.0",
"http": "0.0.0",
"uuid": "^3.0.1",
Expand All @@ -24,6 +25,7 @@
},
"devDependencies": {
"@types/body-parser": "0.0.33",
"@types/cors": "^2.8.0",
"@types/express": "^4.0.34",
"@types/node": "^6.0.56",
"@types/request": "0.0.39",
Expand Down
22 changes: 17 additions & 5 deletions src/expressapp.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as express from 'express';
import * as bodyParser from 'body-parser';
import * as WebSocketServer from 'ws';
import * as cors from 'cors';
import http = require('http');
import { viwiLogger, viwiLoggerInstance } from "./log";

Expand All @@ -24,12 +25,12 @@ class viwiWebSocket {
unsubscribeAck(event:string):void {
this.ws.send(JSON.stringify({type: "unsubscribe", status: "ok", event: event}));
}
}
}



// create server and listen on provided port (on all network interfaces).
class WebServer {
// create server and listen on provided port (on all network interfaces).
class WebServer {
public app: express.Express;
public ws: WebSocketServer.Server;
private _server:any;
Expand All @@ -39,6 +40,17 @@ class WebServer {
constructor (_port?:number) {
this._logger = viwiLogger.getInstance().getLogger("general");
this.app = express();

var whitelist = ['127.0.0.1'];
let corsOpts:cors.CorsOptions = {
origin: function (origin, callback) {
let originIsWhitelisted = whitelist.indexOf(origin) !== -1
callback(originIsWhitelisted ? null : new Error('Bad Request'), originIsWhitelisted)
},
exposedHeaders: 'Location'
}

this.app.use(cors());
this.app.use(bodyParser.json());
this.app.use(bodyParser.urlencoded({ extended: false }));
this.app.use((req:express.Request, res:express.Response, next:express.NextFunction) => {
Expand All @@ -56,8 +68,8 @@ class WebServer {
}

init() {
this._server.listen(this._port);
this._server.on('listening', this.onListening);
this._server.listen(this._port);
this._server.on('listening', this.onListening);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ class viwiLogger {
private _loggers:{[name: string]:viwiLoggerInstance} = {};

constructor() {
if(viwiLogger._instance){
throw new Error("Error: Instantiation failed: Use SingletonClass.getInstance() instead of new.");
}
viwiLogger._instance = this;
if(viwiLogger._instance){
throw new Error("Error: Instantiation failed: Use SingletonClass.getInstance() instead of new.");
}
viwiLogger._instance = this;
}

public static getInstance():viwiLogger
{
return viwiLogger._instance;
return viwiLogger._instance;
}

getLogger(name:string):viwiLoggerInstance {
Expand Down

0 comments on commit ea5208a

Please sign in to comment.