forked from petkivim/nodejs-rest-api-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
50 lines (39 loc) · 1.43 KB
/
Copy pathserver.js
File metadata and controls
50 lines (39 loc) · 1.43 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
'use strict';
var express = require('express');
var app = express();
app.set("port", process.env.PORT || 4000);
app.get('/', function (req, res) {
res.writeHead(200, {'Content-Type': 'application/json'});
var response = { "response" : "This is GET method." }
console.log(response);
res.end(JSON.stringify(response));
})
app.get('/:id', function (req, res) {
res.writeHead(200, {'Content-Type': 'application/json'});
var response = { "response" : "This is GET method with id=" + req.params.id + "." }
console.log(response);
res.end(JSON.stringify(response));
})
app.post('/', function (req, res) {
res.writeHead(200, {'Content-Type': 'application/json'});
var response = { "response" : "This is POST method." }
console.log(response);
res.end(JSON.stringify(response));
})
app.put('/', function (req, res) {
res.writeHead(200, {'Content-Type': 'application/json'});
var response = { "response" : "This is PUT method." }
console.log(response);
res.end(JSON.stringify(response));
})
app.delete('/', function (req, res) {
res.writeHead(200, {'Content-Type': 'application/json'});
var response = { "response" : "This is DELETE method." }
console.log(response);
res.end(JSON.stringify(response));
})
var server = app.listen(app.get("port"), function () {
var host = server.address().address
var port = server.address().port
console.log("Node.js API app listening at http://%s:%s", host, port)
})