-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
45 lines (38 loc) · 1.22 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
const express = require('express');
const redis = require('redis');
// create express application instance
const app = express();
app.use(express.json()); // to support JSON-encoded bodies
app.use(express.urlencoded()); // to support URL-encoded bodies
// create and connect redis client to local instance.
const host = '60.52.126.248';
const port = 6379;
const client = redis.createClient(port, host)
client.auth('mnxfordc');
// echo redis errors to the console
client.on('connect', function() {
console.log('connected');
});
client.on('error', (err) => {
console.log("Error " + err)
});
// get item list
app.get('/get', (request, response) => {
client.get(request.query.key, function (err, reply) {
console.log(reply.toString()); // Will print value
});
});
// set item
app.post('/set', (request, response) => {
var item = {
item_key: request.body.item_key,
item_value: request.body.item_value
};
client.set(item.item_key.toString(), item.item_value.toString(), function(err, reply) {
console.log(reply);
});
});
// start express server at 3000 port
app.listen(4200, () => {
console.log('Server listening on port: ', 4200)
});