forked from sf-wdi-27-28/express-personal-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
66 lines (47 loc) · 1.56 KB
/
Copy pathserver.js
File metadata and controls
66 lines (47 loc) · 1.56 KB
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
// SERVER-SIDE JAVASCRIPT
// Modules and Configuration ////
//require express in our app
var express = require('express'),
app = express(),
bodyParser = require('body-parser');
// serve static files from public folder
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({ extended: true }));
// need to add this so that we can accept request payloads
app.use(bodyParser.json());
// set 'html' as the engine, using ejs's renderFile function
var ejs = require('ejs');
app.engine('html', ejs.renderFile);
app.set('view engine', 'html');
var controllers = require('./controllers');
/**********
* ROUTES *
**********/
/*
* HTML Endpoints
*/
app.get('/', function homepage(req, res) {
res.sendFile(__dirname + '/views/index.html');
});
// app.get('/newEvents', function (req, res) {
// res.sendFile(__dirname + '/views/newEvents.html');
// });
// JSON API Endpoints
// API Controller
app.get('/api', controllers.api.index);
app.get('/api/posts', controllers.posts.index);
app.post('/api/posts', controllers.posts.create);
app.get('/api/posts/:id', controllers.posts.show);
app.delete('/api/posts/:id', controllers.posts.destroy);
app.put('/api/posts/:id', controllers.posts.update);
app.get('templates/:name', controllers.api.templates);
// redirect all other paths to index
app.get('*', function homepage (req, res) {
res.sendFile(__dirname + '/views/index.html');
});
/**********
* SERVER *
**********/
// listen on port 3000
app.listen(process.env.PORT || 3000);
console.log('Express server is up and running on http://localhost:3000/');