-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
77 lines (64 loc) · 2.32 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const config = require('./app/config/config'),
restify = require('restify'),
mysql = require('mysql');
var connection = config.db.get;
/**
* Initialize Server
*/
const server = restify.createServer({
name: config.name,
version: config.version,
url: config.hostname
});
server.use(restify.plugins.acceptParser(server.acceptable));
server.use(restify.plugins.queryParser());
server.use(restify.plugins.bodyParser());
server.get('/main', function (req, res, next) {
res.send('hw');
next();
});
//rest api to get all results
server.get('/vacancies', function (req, res) {
connection.query('select * from vacancies', function (error, results, fields) {
if (error) throw error;
res.end(JSON.stringify(results));
});
});
//rest api to get a single vacancies data
server.get('/vacancies/:id', function (req, res) {
connection.query('select * from vacancies where id=?', [req.params.id], function (error, results, fields) {
if (error) throw error;
res.end(JSON.stringify(results));
});
});
//rest api to create a new record into mysql database
server.post('/vacancies', function (req, res) {
var postData = req.body;
connection.query('INSERT INTO vacancies SET ?', postData, function (error, results, fields) {
if (error) throw error;
res.end(JSON.stringify(results));
});
});
//rest api to update record into mysql database
server.put('/vacancies', function (req, res) {
connection.query('UPDATE `vacancies` SET `vacancies_name`=?,`pay`=?,`skill`=?,`candidate`=? where `Id`=?',
[req.body.vacancies_name, req.body.pay, req.body.skill, req.body.candidate, req.body.id], function (error, results, fields) {
if (error) throw error;
res.end(JSON.stringify(results));
});
});
//rest api to delete record from mysql database
server.del('/vacancies/:id', function (req, res) {
connection.query('DELETE FROM `vacancies` WHERE `id`=?', [req.params.id], function (error, results, fields) {
if (error) throw error;
res.end('Record has been deleted!');
});
});
server.listen(8070, function () {
console.log('%s listening at %s', server.name, server.url);
});
server.get('/\/.*/', restify.plugins.serveStatic({
directory: __dirname + "/public/",
default: './HR_APP_Page_Mobile.html'
})
);