-
Notifications
You must be signed in to change notification settings - Fork 0
/
api-server.js
51 lines (43 loc) · 1.23 KB
/
api-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
const restify = require('restify');
const corsMiddleware = require('restify-cors-middleware');
const ip_addr = '127.0.0.1';
const port = '8888';
var server = restify.createServer();
server.use(require('restify-plugins').queryParser());
const cors = corsMiddleware({
preflightMaxAge: 5, //Optional
origins: ['*']
});
server.pre(cors.preflight);
server.use(cors.actual);
var PATH = '/greetings';
server.get({ path: PATH }, getGreetings);
function getGreetings(req, res, next) {
// res.setHeader('Access-Control-Allow-Origin','*');
res.send(200, {
data: [
{
id: 1,
personalGreet: 'Hello, John Doe',
},
{
id: 2,
personalGreet: 'Hi, Max Doe'
}
]
});
}
/*
//Get request with a param
server.get({path: PATH + '/greeting/:id'}, getGreetingsDetails);
function getGreetingsDetails(req, res) {
if (!req.params.id) {
res.send(400);
} else {
// prepare response
// or do a call to an external api with axios: axios.get().then(resp => {}).catch(error => {});
}
}*/
server.listen(port, ip_addr, function () {
console.log('%s listening at %s ', server.name, server.url);
});