5e4b076b93
* Changed unsaved changes asterisk to an svg circle. #158 * Fixed #100 Unmatched routes are handled by react-router on the client side and a single wildcard route on server.routes.js renders the index html. When the /:username/sketches route is matched and the username is not valid, the user will be redirected to the index route and a toast will explain what happened. When the username is 'p5' (default when logged out) it will show all sketches. Maybe this should be changed to just public or 'local' sketches? * Moved unsaved changes SVG to a separate file. * User not found is now a 404 error. * Added server rendered 404 page. * Removed console.log * 404 Page now renders a random p5 sketch. TODO: make 404 sketches. * Added 404 header 404 page now fetches a random example sketch * Moved circle closer to file name * Render 404 page in SketchList route if !user
113 lines
3.2 KiB
JavaScript
113 lines
3.2 KiB
JavaScript
import Express from 'express';
|
|
import mongoose from 'mongoose';
|
|
import bodyParser from 'body-parser';
|
|
import cookieParser from 'cookie-parser';
|
|
import session from 'express-session';
|
|
const MongoStore = require('connect-mongo')(session);
|
|
import passport from 'passport';
|
|
import path from 'path';
|
|
|
|
// Webpack Requirements
|
|
import webpack from 'webpack';
|
|
import config from '../webpack.config.dev';
|
|
import webpackDevMiddleware from 'webpack-dev-middleware';
|
|
import webpackHotMiddleware from 'webpack-hot-middleware';
|
|
|
|
const app = new Express();
|
|
|
|
// Run Webpack dev server in development mode
|
|
if (process.env.NODE_ENV === 'development') {
|
|
const compiler = webpack(config);
|
|
app.use(webpackDevMiddleware(compiler, { noInfo: true, publicPath: config.output.publicPath }));
|
|
app.use(webpackHotMiddleware(compiler));
|
|
}
|
|
|
|
// Import all required modules
|
|
import serverConfig from './config';
|
|
import users from './routes/user.routes';
|
|
import sessions from './routes/session.routes';
|
|
import projects from './routes/project.routes';
|
|
import files from './routes/file.routes';
|
|
import aws from './routes/aws.routes';
|
|
import serverRoutes from './routes/server.routes';
|
|
import embedRoutes from './routes/embed.routes';
|
|
|
|
import { renderIndex } from './views/index';
|
|
import { get404Sketch } from './views/404Page';
|
|
|
|
// Body parser, cookie parser, sessions, serve public assets
|
|
|
|
app.use(Express.static(path.resolve(__dirname, '../static')));
|
|
app.use(bodyParser.urlencoded({ extended: true }));
|
|
app.use(bodyParser.json());
|
|
app.use(cookieParser());
|
|
app.use(session({
|
|
resave: true,
|
|
saveUninitialized: false,
|
|
secret: process.env.SESSION_SECRET,
|
|
proxy: true,
|
|
name: 'sessionId',
|
|
cookie: {
|
|
httpOnly: true,
|
|
secure: false,
|
|
},
|
|
store: new MongoStore({
|
|
url: process.env.MONGO_URL,
|
|
autoReconnect: true
|
|
})
|
|
}));
|
|
app.use(passport.initialize());
|
|
app.use(passport.session());
|
|
app.use('/api', users);
|
|
app.use('/api', sessions);
|
|
app.use('/api', projects);
|
|
app.use('/api', files);
|
|
app.use('/api', aws);
|
|
// this is supposed to be TEMPORARY -- until i figure out
|
|
// isomorphic rendering
|
|
app.use('/', serverRoutes);
|
|
|
|
app.use('/', embedRoutes);
|
|
app.get('/auth/github', passport.authenticate('github'));
|
|
app.get('/auth/github/callback', passport.authenticate('github', { failureRedirect: '/login' }), (req, res) => {
|
|
res.redirect('/');
|
|
});
|
|
|
|
// configure passport
|
|
require('./config/passport');
|
|
// const passportConfig = require('./config/passport');
|
|
|
|
// Connect to MongoDB
|
|
// mongoose.connect(process.env.MONGODB_URI || process.env.MONGOLAB_URI);
|
|
mongoose.connect(serverConfig.mongoURL);
|
|
mongoose.connection.on('error', () => {
|
|
console.error('MongoDB Connection Error. Please make sure that MongoDB is running.');
|
|
process.exit(1);
|
|
});
|
|
|
|
app.get('/', (req, res) => {
|
|
res.sendFile(renderIndex());
|
|
});
|
|
|
|
// Handle missing routes.
|
|
app.get('*', (req, res) => {
|
|
res.status(404);
|
|
if (req.accepts('html')) {
|
|
get404Sketch(html => res.send(html));
|
|
return;
|
|
}
|
|
if (req.accepts('json')) {
|
|
res.send({ error: 'Not found.' });
|
|
return;
|
|
}
|
|
res.type('txt').send('Not found.');
|
|
});
|
|
|
|
// start app
|
|
app.listen(serverConfig.port, (error) => {
|
|
if (!error) {
|
|
console.log(`p5js web editor is running on port: ${serverConfig.port}!`); // eslint-disable-line
|
|
}
|
|
});
|
|
|
|
export default app;
|