This repository has been archived by the owner on Apr 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
64 lines (51 loc) · 1.82 KB
/
app.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const mongoose = require('mongoose');
const path = require('path');
const bodyParser = require('body-parser');
const register = require('./routes/register');
const logIn = require('./routes/logIn');
const logOut = require('./routes/logOut');
const newPost = require('./routes/newPost');
const article = require('./routes/article');
const profile = require('./routes/profile');
const posts = require('./routes/posts');
const tags = require('./routes/tags');
const express = require('express');
const app = express();
const session = require('express-session');
const fs = require('fs')
mongoose.connect('mongodb://hung131:abc123@ds151383.mlab.com:51383/simple-blog-db');
app.use(bodyParser.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'dist')));
require('./prod')(app);
app.use(session({
secret: 'very mysterious',
saveUninitialized: false,
resave: false }))
// GET route for reading data
app.use('/api/posts', posts);
//POST route for register new user
app.use('/register', register);
// POST log in after registering
app.use('/api/log-in', logIn);
// User log out POST to clear session
app.use('/api/log-out', logOut);
// Handle one article
app.use('/article', article)
//POST method for new post
app.use('/api/new-post', newPost);
// GET articles for a tag
app.use('/tag', tags);
//POST method finds all posts of one author
app.use('/profile', profile);
app.use('/*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/index.html'))
})
// app.use('*', async function (req, res) {
// const indexFile = fs.readFileSync(path.join(__dirname, 'dist/index.html')).toString()
// res.writeHead(200, {
// 'Content-Type': 'text/html; charset=utf-8'
// })
// res.end(indexFile)
// });
app.listen(3000 || process.env.PORT, () => console.log('Listening...'));