-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·54 lines (43 loc) · 1.65 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
/* server.js - Express server*/
'use strict';
const log = console.log
log('Express server')
const express = require('express')
const app = express();
const path = require('path');
// Setting up a static directory for the files in /pub
// using Express middleware.
// Don't put anything in /pub that you don't want the public to have access to!
app.use(express.static(path.join(__dirname, '/pub')))
// Let's make some express 'routes'
// Express has something called a Router, which
// takes specific HTTP requests and handles them
// based on the HTTP method and URL
// Let's make a route for an HTTP GET request to the
// 'root' of our app (i.e. top level domain '/')
app.get('/', (req, res) => {
// sending a string
//res.send('This should be the root route!')
//sending some HTML
res.sendFile(path.join(__dirname + '/pub/examples.html'))
})
// Error codes
app.get('/problem', (req, res) => {
// You can indicate a status code to send back
// by default it is 200, but it's up to you
// if you want to send something
res.status(500).send('There was a problem on the server')
// don't send nonsense status codes like this one:
//res.status(867).send('There was a problem on the server')
})
// Sending some JSON
app.get('/someJSON', (req, res) => {
// object converted to JSON string
})
// will use an 'environmental variable', process.env.PORT, for deployment.
const port = process.env.PORT || 5000
app.listen(port, () => {
log(`Listening on port ${port}...`)
}) // localhost development port 5000 (http://localhost:5000)
// We've bound that port to localhost to go to our express server.
// Must restart web server when you make changes to route handlers.