-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.mjs
41 lines (32 loc) · 1.17 KB
/
server.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import express from 'express';
import mongoose from 'mongoose';
import { newSnippet, createSnippet, getSnippetById, getRawSnippetById } from './controllers/snippetController.mjs';
// Initialize express app.
const app = express();
// Establish conenction with MongoDB.
mongoose.connect("mongodb://127.0.0.1/bitbin", {
useUnifiedTopology: true,
useNewUrlParser: true
}).then(()=> console.log('connected'))
// Set EJS as the templating language and view engine.
app.set('view engine', 'ejs');
// Properly parse request body.
app.use(express.urlencoded({ extended: true }));
// Serve static files in /public directory.
app.use(express.static('public'));
// Set routes
app.get('/', (req, res) => {
const code = `Welcome to BitBin!
Use the commands in the top right corner
to create a new file to share with others.`;
res.render('snippet', { code, id: 'abc' });
});
app.get('/new', newSnippet);
app.post('/save', createSnippet);
app.get('/raw/:id', getRawSnippetById);
app.get('/:id', getSnippetById);
const PORT = process.env.PORT || 3000;
// Run the express app by listening to `PORT`.
app.listen(PORT, () => {
console.log(`Listening on https://localhost:${PORT}`);
});