-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy path3_express-server-routing.js
More file actions
47 lines (35 loc) · 1.41 KB
/
3_express-server-routing.js
File metadata and controls
47 lines (35 loc) · 1.41 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
/*
Routing with Express.js
Routing is how we point an HTTP request to our code. If you go to a
URL, your server will likely receive a GET request (there are more
types of requests), and you will send a response, probably your
site content.
*/
const express = require('express');
const app = express();
var port = 3001;
/*
Below we are defining how we handle a specific request for a specific
URI.
URI: Uniform Resource Identifier, good discussion of URI, URL, and URN:
http://stackoverflow.com/questions/176264/what-is-the-difference-between-a-uri-a-url-and-a-urn)
*/
// This defines the route pointing to the root of the app
// Go to localhost:3000 and you will receive this response
app.get('/', function(request, response) {
response.send('Hello World!')
});
// If the client is sending a GET request to the server, the server will
// respond with the callback (second argument)
// Go to localhost:3000/myURI to get this response
app.get('/supercool', function(request, response) {
response.send('Responding to a supercool GET request!');
});
// If the client is attempting to submit data to the server via POST
// We will process that request and send back a response below
// It is not quite as easy to just send a POST request right now, we will
// figure that out later.
app.post('/myURI', function(request, response) {
response.send('Responding to a POST request!');
});
app.listen(port);