Guest User

Untitled

a guest
Jan 12th, 2019
833
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. CORS issue on express/NodeJS,sevices not available from Internet Explorer
  2. var config = require('./config.js');
  3.  
  4. exports.setup = function (params) {
  5.  
  6. var controllers = params.controllers;
  7. var app = params.app;
  8.  
  9. // CORS (Cross Origin Resource Sharing) Implementation
  10. app.all('/*', function(req, res, next) {
  11. res.header("Access-Control-Allow-Credentials", config.responseSettings.AccessControlAllowCredentials);
  12. res.header("Access-Control-Allow-Origin", (req.headers.origin) ? req.headers.origin : config.responseSettings.AccessControlAllowOrigin);
  13. res.header("Access-Control-Allow-Headers", (req.headers['access-control-request-headers']) ? req.headers['access-control-request-headers'] : "x-requested-with");
  14. res.header("Access-Control-Allow-Methods", (req.headers['access-control-request-method']) ? req.headers['access-control-request-method'] : config.responseSettings.AccessControlAllowMethods);
  15. next();
  16. });
  17.  
  18. app.get('/', function(req, res) {
  19. res.render('index', { title: 'Welcome })
  20. });
  21.  
  22.  
  23.  
  24. function auth(req, res, next) {
  25. if (req.session.UserId || (req.query.apikey && config.apikeys.indexOf(req.query.apikey) > -1)) {
  26. next();
  27. } else {
  28. res.send(401);
  29. }
  30. }
  31.  
  32. app.get('/Session/:id?', controllers.SessionController.getSession);
  33. app.post('/Session', controllers.SessionController.createSession);
  34. app.del('/Session/:id', controllers.SessionController.deleteSession);
  35. ...
  36. }
  37.  
  38. module.exports = {
  39. "db": {
  40. "mongodb": "mongodb://admin:XYX123@localhost/xyx",
  41. "username": "abc",
  42. "password": "abc123",
  43. "database": "abcdb",
  44. "server": "localhost"
  45. },
  46. "cookiesecret": "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz",
  47. "responseSettings": {
  48. "AccessControlAllowOrigin": "*",
  49. "AccessControlAllowHeaders": "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version",
  50. "AccessControlAllowMethods": "POST,GET,PUT,DELETE",
  51. "AccessControlAllowCredentials": true
  52. },
  53. "apikeys": ['587c57365b54e8283fd6b1ac24acf29d', '4de04266bdd87410de698cfc33c55d68', '232c0252cee5e97148636ee2efd6ee94'], //only 1 is used now
  54.  
  55. };
  56.  
  57. app.configure(function () {
  58. app.set('views', __dirname + '/views');
  59. app.set('view engine', 'jade');
  60. app.use(express.bodyParser());
  61. app.use(express.methodOverride());
  62. app.use(express.cookieParser());
  63. app.use(express.session({ // to set a time here only for session expire
  64. secret: config.cookiesecret,
  65. store: new MongoStore({ db: config.db.database, host: config.db.server, username: config.db.username, password: config.db.password })
  66. }));
  67. app.use(app.router);
  68. app.use(express.static(__dirname + '/public'));
  69. });
  70.  
  71. app.configure('development', function () {
  72. app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
  73. });
  74.  
  75. app.configure('production', function () {
  76. app.use(express.errorHandler());
  77. });
  78.  
  79. // Routes
  80.  
  81. routes.setup({
  82. 'controllers': controllers,
  83. 'app': app
  84. });
  85.  
  86. app.listen(process.env.port || 3000);
  87. console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
  88.  
  89. var SessionModel = Backbone.Model.extend({
  90.  
  91. urlRoot: config.BaseUrl + '/Session',
  92.  
  93. initialize: function () {
  94.  
  95. var that = this;
  96.  
  97. $.ajaxPrefilter(function (options, originalOptions, jqXHR) {
  98.  
  99. options.xhrFields = {
  100. withCredentials: true
  101. };
  102. })
  103.  
  104. },
  105.  
  106. login: function (creds, callback) {
  107.  
  108. // Do a POST to /session and send the serialized form creds
  109. this.save(creds, {
  110. success: callback
  111. });
  112. },
  113.  
  114. logout: function (callback) {
  115. // Do a DELETE to /session and clear the clientside data
  116.  
  117. var that = this;
  118. this.destroy({
  119. success: function (model, resp) {
  120. model.clear()
  121. model.id = null;
  122.  
  123. // Set auth to false to trigger a change:auth event
  124. // The server also returns a new csrf token so that
  125. // the user can relogin without refreshing the page
  126.  
  127. that.set({ auth: false });
  128. callback();
  129. }
  130. });
  131. },
  132.  
  133. getAuth: function (callback) {
  134.  
  135. // getAuth is wrapped around our router
  136. // before we start any routers let us see if the user is valid
  137. this.fetch({
  138.  
  139. //success: callback
  140. success: function (req, res) {
  141. //alert("success");
  142. callback();
  143. },
  144. error: function (err) {
  145. //alert("error");
  146. callback();
  147. }
  148. });
  149. }
  150.  
  151. });
  152.  
  153. return new SessionModel;
  154. });
Add Comment
Please, Sign In to add comment