forked from SF-WDI-LABS/express-personal-api
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathserver.js
68 lines (55 loc) · 2.01 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
// require express and other modules
var express = require('express'),
app = express();
// parse incoming urlencoded form data
// and populate the req.body object
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
// allow cross origin requests (optional)
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
/************
* DATABASE *
************/
// var db = require('./models');
/**********
* ROUTES *
**********/
// Serve static files from the `/public` directory:
// i.e. `/images`, `/scripts`, `/styles`
app.use(express.static('public'));
/*
* HTML Endpoints
*/
app.get('/', function homepage(req, res) {
res.sendFile(__dirname + '/views/index.html');
});
/*
* JSON API Endpoints
*/
app.get('/api', function apiIndex(req, res) {
// TODO: Document all your api endpoints below as a simple hardcoded JSON object.
// It would be seriously overkill to save any of this to your database.
res.json({
woopsIForgotToDocumentAllMyEndpoints: true, // CHANGE ME ;)
message: "Welcome to my personal api! Here's what you need to know!",
documentationUrl: "https://github.com/example-username/express-personal-api/README.md", // CHANGE ME
baseUrl: "http://YOUR-APP-NAME.herokuapp.com", // CHANGE ME
endpoints: [
{method: "GET", path: "/api", description: "Describes all available endpoints"},
{method: "GET", path: "/api/profile", description: "Data about me"}, // CHANGE ME
{method: "POST", path: "/api/campsites", description: "E.g. Create a new campsite"} // CHANGE ME
]
})
});
/**********
* SERVER *
**********/
// listen on the port that Heroku prescribes (process.env.PORT) OR port 3000
app.listen(process.env.PORT || 3000, function () {
console.log('Express server is up and running on http://localhost:3000/');
});