Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. import path from 'path';
  2. import {Server} from 'https';
  3. import Express from 'express';
  4. import bodyParser from 'body-parser';
  5. import React from 'react';
  6. import {renderToString} from 'react-dom/server';
  7. import {match,RouterContext} from 'react-router';
  8. import routes from './routes';
  9. import NotFoundPage from './pages/NotFoundPage';
  10. import stormpath from 'express-stormpath';
  11. import fs from 'fs';
  12. var privateKey = fs.readFileSync('/var/www/justsearch.co/src/server.key', 'utf8');
  13. var certificate = fs.readFileSync('/var/www/justsearch.co/src/server.crt', 'utf8');
  14. var ca = fs.readFileSync('/var/www/justsearch.co/src/justsearch_co.ca-bundle', 'utf8');
  15.  
  16. var credentials = {enable: true,ca: ca, key: privateKey, cert: certificate};
  17.  
  18. // initialize the server and configure support for ejs templates
  19. const app = new Express();
  20. const server = new Server(app);
  21. app.set('view engine','ejs');
  22. app.set('views',path.join(__dirname,'views'));
  23.  
  24.  
  25. // define the folder that will be used for static assets
  26. app.use(Express.static(path.join(__dirname,'static')));
  27.  
  28. app.use(stormpath.init(app,{
  29. web: {
  30. produces: ['application/json']
  31. },
  32. client: {
  33. apiKey: {
  34. id: 'HVEQTIJB2B5RBEWN4LPP7FUVX',
  35. secret: '0b9nQdDmD0/sJpG15hM6Vjv22v16BPNiiDGWPPPFseo'
  36. }
  37. },
  38. application: {
  39. href: 'https://api.stormpath.com/v1/applications/39POMWYg0hawy75ZGVA36b'
  40. }
  41. }))
  42.  
  43. // //handling New User Requests
  44.  
  45. // app.post('/me', bodyParser.json(), stormpath.loginRequired, function (req, res) {
  46. // function writeError(message) {
  47. // res.status(400);
  48. // res.json({ message: message, status: 400 });
  49. // res.end();
  50. // }
  51.  
  52. // function saveAccount () {
  53. // req.user.givenName = req.body.givenName;
  54. // req.user.surname = req.body.surname;
  55. // req.user.email = req.body.email;
  56.  
  57. // req.user.save(function (err) {
  58. // if (err) {
  59. // return writeError(err.userMessage || err.message);
  60. // }
  61. // res.end();
  62. // });
  63. // }
  64.  
  65. // if (req.body.password) {
  66. // var application = req.app.get('stormpathApplication');
  67.  
  68. // application.authenticateAccount({
  69. // username: req.user.username,
  70. // password: req.body.existingPassword
  71. // }, function (err) {
  72. // if (err) {
  73. // return writeError('The existing password that you entered was incorrect.');
  74. // }
  75.  
  76. // req.user.password = req.body.password;
  77.  
  78. // saveAccount();
  79. // });
  80. // } else {
  81. // saveAccount();
  82. // }
  83. // });
  84.  
  85.  
  86. //universal routing and rendering
  87.  
  88.  
  89. app.get('*',(req,res)=>{
  90. match(
  91. {routes,location: req.url},
  92. (err, redirectLocation, renderProps) => {
  93. // in case of error display the error message
  94. if (err) {
  95. return res.status(500).send(err.message);
  96. }
  97.  
  98. //in case of redirect propagate the redirect to the browser
  99. if (redirectLocation){
  100. return res.redirect(302,redirectLocation.pathname + redirectLocation.search)
  101. }
  102.  
  103. // generate the React markup for the current route
  104. let markup;
  105. if (renderProps){
  106. // if the current route matched we have renderProps
  107. markup = renderToString(<RouterContext {...renderProps}/>);
  108. }else{
  109. //otherwise we can render a 404 page
  110. markup = renderToString(<NotFoundPage/>);
  111. res.status(404);
  112. }
  113. //render the index template with the embedded react markup
  114. return res.render('index',{markup});
  115.  
  116. }
  117. );
  118. });
  119.  
  120. //Start Server
  121.  
  122. // const port = process.env.PORT || 8888;
  123. // const env = process.env.NOV_ENV || 'production';
  124. var httpsServer = server.createServer(credentials, app);
  125. httpsServer.listen(8443);
  126.  
  127. // server.listen(port, err=>{
  128. // if (err){
  129. // return console.error(err);
  130. // }
  131. // console.info("Server running on http://localhost:" + port + env);
  132. // })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement