Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ MAILGUN_KEY=<your-mailgun-api-key>
ML5_LIBRARY_USERNAME=ml5
ML5_LIBRARY_EMAIL=examples@ml5js.org
ML5_LIBRARY_PASS=helloml5
MONGO_URL=mongodb://localhost:27017/p5js-web-editor
MONGO_URI=mongodb://localhost:27017/p5js-web-editor
PORT=8000
PREVIEW_PORT=8002
EDITOR_URL=http://localhost:8000
Expand Down
47 changes: 6 additions & 41 deletions server/previewServer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Express from 'express';
import mongoose from 'mongoose';
import path from 'path';
import webpack from 'webpack';
import cors from 'cors';
Expand All @@ -12,54 +11,24 @@ import { renderPreviewIndex } from './views/previewIndex';

const app = new Express();

// This also works if you take out the mongoose connection
// but i have no idea why
const mongoConnectionString = process.env.MONGO_URL;

const connectToMongoDB = async () => {
try {
mongoose.set('strictQuery', true);

await mongoose.connect(mongoConnectionString, {
serverSelectionTimeoutMS: 30000,
socketTimeoutMS: 45000
});
} catch (error) {
console.error('Failed to connect to MongoDB:', error);
process.exit(1);
}
};

connectToMongoDB();

mongoose.connection.on('error', (err) => {
console.error(
'MongoDB Connection Error. Please make sure that MongoDB is running.',
err
);
});

const allowedCorsOrigins = [
/p5js\.org$/,
process.env.EDITOR_URL,
process.env.PREVIEW_URL
];

// to allow client-only development
if (process.env.CORS_ALLOW_LOCALHOST === 'true') {
allowedCorsOrigins.push(/localhost/);
}

// Enable Cross-Origin Resource Sharing (CORS)
const corsMiddleware = cors({
credentials: true,
origin: allowedCorsOrigins
});

app.use(corsMiddleware);
// Enable pre-flight OPTIONS route for all end-points
app.options('*', corsMiddleware);

// Run Webpack dev server in development mode
if (process.env.NODE_ENV === 'development') {
const compiler = webpack(config);
app.use(
Expand All @@ -85,18 +54,14 @@ app.get('/', (req, res) => {
app.use('/', embedRoutes);
app.use('/', assetRoutes);

// Handle missing routes.
app.get('*', (req, res) => {
res.status(404);
res.type('txt').send('Not found.');
res.status(404).type('txt').send('Not found.');
});

app.listen(process.env.PREVIEW_PORT, (error) => {
if (!error) {
console.log(
`p5.js Preview Server is running on port: ${process.env.PREVIEW_PORT}`
);
}
app.listen(process.env.PREVIEW_PORT, () => {
console.log(
`p5.js Preview Server is running on port: ${process.env.PREVIEW_PORT}`
);
});

export default app;
5 changes: 2 additions & 3 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ if (process.env.NODE_ENV === 'development') {
app.use(webpackHotMiddleware(compiler, { log: false }));
}

const mongoConnectionString = process.env.MONGO_URL;
const mongoConnectionString = process.env.MONGO_URI || process.env.MONGO_URL;
app.set('trust proxy', true);

// Enable Cross-Origin Resource Sharing (CORS) for all origins
Expand Down Expand Up @@ -95,8 +95,7 @@ app.use(
maxAge: 1000 * 60 * 60 * 24 * 28 // 4 weeks in milliseconds
},
store: MongoStore.create({
mongoUrl: mongoConnectionString,
ttl: 1000 * 60 * 60 * 24 * 28 // 4 weeks in milliseconds to match cookie maxAge
mongoUrl: mongoConnectionString
})
})
);
Expand Down
Loading