-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
96 lines (75 loc) · 2.71 KB
/
server.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* create express server
*/
const express = require( "express" )
/**
* for the mongoDB connection
*/
const mongoose = require( "mongoose" )
/**
* MongoDB Collection: Article
*/
const Article = require( "./models/article" )
/**
* routes for article
*/
const articleRouter = require( "./routes/articles" )
require( "dotenv" ).config()
/**
* Create a new middleware function to override the req.method property with a new value.
* It is very important that this module is used before any module that needs to know the method of the request (for example, it must be used prior to the csurf module).
*/
//Lets you use HTTP verbs such as PUT or DELETE in places where the client doesn't support it.
//We did the DELETE Button in a form because google scrolles the site and clicks every link. SO it is not possible to use a link!!!! Instead you use a form with a button inside. Becuase you obly can use GET and POST in a form, we use methodOverride
const methodOverride = require( "method-override" )
/**
* create express server
*/
const app = express();
/**
* connect with mongoDB
*/
mongoose.connect( "mongodb+srv://admin:123@cluster0.1e0z1.mongodb.net/myFirstDatabase?retryWrites=true&w=majority", { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true } )
/**
* change view engine
*/
app.set( "view engine", "ejs" )
/**
* we use a form and input via post: put this above the "app.use( " / articles", articleRouter )"
*/
app.use( express.urlencoded( { extended: false } ) )
//It is very important that this module is used before any module that needs to know the method of the request (for example, it must be used prior to the csurf module).
app.use( methodOverride( "_method" ) )
//add router from module with a new path.
app.use( "/articles", articleRouter );
/**
* change view folder
*/
//app.set( "view", __dirname + "/view" )
/**
* ROUTES
*/
app.get( "/", async ( req, res ) => {
//OLD VERSION
// const articles = [{
// title: "Test Article",
// date: new Date().toLocaleDateString(),
// description: "Lorem",
//
// }, {
// title: "Test Article 2",
// date: new Date().toLocaleDateString(),
// description: "Lorem sdf sdfsdf sdf s df sdfsdf sg f hg jhi kk jfgd gs d as",
//
// }]
try{
// get all Articles from mongoDB, sorted by date
const articles = await Article.find( {} ).sort( [['date', -1]] )
//shie index.ejs with data from articles
res.render( "articles/index", { articles: articles } )
} catch (err) {
// handle the error safely
console.log(err)
}
})
app.listen( process.env.PORT || 3002, () => { console.log( "connected to db" ,process.env.PORT || 3002); } )