Tuesday, 16 October 2018

How to enable CORS in an existing nodejs app(widestage)

I tried to get support on the WideStage slack channel: widestage.slack.com but it doesn't look like there is a lot of responses from the devs just people posting questions/problems.​All I need is to enable CORS so that my servers on different domains can communicate/call the widestage api but I am unsure how to do it, I have googled it and seen examples but I don't know enough about nodejs to figure out how to edit an existing nodejs server.js file with views and a bunch of other stuff to make CORS work.​I looked at answer on stack overflow but its way over my head how to take the answer and make it work with the below code.https://stackoverflow.com/questions/7067966/how-to-allow-cors​Any help would be greatly appreciated.​Here is the repo: https://github.com/widestage/widestage​This is the server.js file:​`` var env = process.env.NODE_ENV || 'production';// Application Paramsprocess.argv.forEach(function(val, index, array) {if (index == 2) env = val;});global.env = env;​// production onlyif (env == 'production') {​};​​var express = require('express'),path = require('path'),http = require('http');​​​var cluster = require('cluster');var passport = require("passport");//var mongoose = require('mongoose');var session = require('express-session');var RedisStore = require('connect-redis')(session); //npm install connect-redis --- to store variable sessionsvar cookieParser = require('cookie-parser');var cookieSession = require('cookie-session');​//var bb = require('express-busboy');var app = express();//bb.extend(app);​// set the view engine to ejsapp.set('view engine', 'ejs');app.set('views', __dirname + '/views');app.use(express.static(path.join(__dirname, 'public')));app.use(express.static(path.join(__dirname, 'bower_components')));​app.use(cookieParser());app.use(cookieSession({key:"widestage", secret:"HEWÑÑasdfwejñlkjqwernnkkk13134134wer", httpOnly: true, secure: false, cookie: {maxAge: 60 * 60 * 1000}}));app.use(session({secret: 'ndwidestagev0', cookie: {httpOnly: true, secure: false}, store: new RedisStore({maxAge: 60 * 60 * 1000}), resave: false, saveUninitialized: true}));​var bodyParser = require('body-parser');app.use(bodyParser.json({limit: '50mb'})); // get information from html formsapp.use(bodyParser.urlencoded({ extended: true }));​var multer = require('multer');//app.use(multer());app.use(multer({dest:'./uploads'}).any());​var authentication = true;​global.authentication = authentication;global.logFailLogin = true;global.logSuccessLogin = true;​if (authentication){app.use(passport.initialize());app.use(passport.session());app.use(passport.authenticate('remember-me'));}​if (cluster.isMaster) {var numCPUs = require('os').cpus().length;​// Fork workers.for (var i = 0; i < numCPUs; i++) {cluster.fork();}​cluster.on('exit', function(deadWorker, code, signal) {var worker = cluster.fork();​// Note the process IDsvar newPID = worker.process.pid;var oldPID = deadWorker.process.pid;​​});} else {var config = require('./server/config/config')[env];global.config = config;​require('./server/config/mongoose')();require('./server/config/passport')(passport);​require('./server/globals');require('./server/config/mailer');​require('./server/config/routes')(app, passport);​var fs = require('fs');​//Custom routesvar routes_dir = __dirname + '/server/custom';fs.readdirSync(routes_dir).forEach(function (file) {if(file[0] === '.') return;require(routes_dir+'/'+ file+'/routes.js')(app);});var ipaddr = process.env.IP || config.ip;var port = process.env.PORT || config.port;​app.listen(port, ipaddr);​console.log("Server running at http://" + ipaddr + ":" + port + "/ in worker "+cluster.worker.id);}​``

Submitted October 16, 2018 at 09:41PM by unbalancedmindx

No comments:

Post a Comment