Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import path from 'path';
- import {Server} from 'https';
- import Express from 'express';
- import bodyParser from 'body-parser';
- import React from 'react';
- import {renderToString} from 'react-dom/server';
- import {match,RouterContext} from 'react-router';
- import routes from './routes';
- import NotFoundPage from './pages/NotFoundPage';
- import stormpath from 'express-stormpath';
- import fs from 'fs';
- var privateKey = fs.readFileSync('/var/www/justsearch.co/src/server.key', 'utf8');
- var certificate = fs.readFileSync('/var/www/justsearch.co/src/server.crt', 'utf8');
- var ca = fs.readFileSync('/var/www/justsearch.co/src/justsearch_co.ca-bundle', 'utf8');
- var credentials = {enable: true,ca: ca, key: privateKey, cert: certificate};
- // initialize the server and configure support for ejs templates
- const app = new Express();
- const server = new Server(app);
- app.set('view engine','ejs');
- app.set('views',path.join(__dirname,'views'));
- // define the folder that will be used for static assets
- app.use(Express.static(path.join(__dirname,'static')));
- app.use(stormpath.init(app,{
- web: {
- produces: ['application/json']
- },
- client: {
- apiKey: {
- id: 'HVEQTIJB2B5RBEWN4LPP7FUVX',
- secret: '0b9nQdDmD0/sJpG15hM6Vjv22v16BPNiiDGWPPPFseo'
- }
- },
- application: {
- href: 'https://api.stormpath.com/v1/applications/39POMWYg0hawy75ZGVA36b'
- }
- }))
- // //handling New User Requests
- // app.post('/me', bodyParser.json(), stormpath.loginRequired, function (req, res) {
- // function writeError(message) {
- // res.status(400);
- // res.json({ message: message, status: 400 });
- // res.end();
- // }
- // function saveAccount () {
- // req.user.givenName = req.body.givenName;
- // req.user.surname = req.body.surname;
- // req.user.email = req.body.email;
- // req.user.save(function (err) {
- // if (err) {
- // return writeError(err.userMessage || err.message);
- // }
- // res.end();
- // });
- // }
- // if (req.body.password) {
- // var application = req.app.get('stormpathApplication');
- // application.authenticateAccount({
- // username: req.user.username,
- // password: req.body.existingPassword
- // }, function (err) {
- // if (err) {
- // return writeError('The existing password that you entered was incorrect.');
- // }
- // req.user.password = req.body.password;
- // saveAccount();
- // });
- // } else {
- // saveAccount();
- // }
- // });
- //universal routing and rendering
- app.get('*',(req,res)=>{
- match(
- {routes,location: req.url},
- (err, redirectLocation, renderProps) => {
- // in case of error display the error message
- if (err) {
- return res.status(500).send(err.message);
- }
- //in case of redirect propagate the redirect to the browser
- if (redirectLocation){
- return res.redirect(302,redirectLocation.pathname + redirectLocation.search)
- }
- // generate the React markup for the current route
- let markup;
- if (renderProps){
- // if the current route matched we have renderProps
- markup = renderToString(<RouterContext {...renderProps}/>);
- }else{
- //otherwise we can render a 404 page
- markup = renderToString(<NotFoundPage/>);
- res.status(404);
- }
- //render the index template with the embedded react markup
- return res.render('index',{markup});
- }
- );
- });
- //Start Server
- // const port = process.env.PORT || 8888;
- // const env = process.env.NOV_ENV || 'production';
- var httpsServer = server.createServer(credentials, app);
- httpsServer.listen(8443);
- // server.listen(port, err=>{
- // if (err){
- // return console.error(err);
- // }
- // console.info("Server running on http://localhost:" + port + env);
- // })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement