-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
39 lines (32 loc) · 1.3 KB
/
index.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
// Require dependencies
var path = require('path');
var express = require('express');
// Declare application parameters
var PORT = process.env.PORT || 3000;
var STATIC_ROOT = path.resolve(__dirname, './public');
// Defining CORS middleware to enable CORS.
// (should really be using "express-cors",
// but this function is provided to show what is really going on when we say "we enable CORS")
function cors(req, res, next){
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Methods", "GET,POST,DELETE,OPTIONS,PUT");
next();
}
// Instantiate an express.js application
var app = express();
// Configure the app to use a bunch of middlewares
app.use(express.json()); // handles JSON payload
app.use(express.urlencoded({ extended : true })); // handles URL encoded payload
app.use(cors); // Enable CORS
app.use('/', express.static(STATIC_ROOT)); // Serve STATIC_ROOT at URL "/" as a static resource
// Configure '/products' endpoint
app.get('/products', function(request, response) {
response.json({
Example: 'This is an Example!'
});
});
// Start listening on TCP port
app.listen(PORT, function(){
console.log('Express.js server started, listening on PORT '+PORT);
});