-
Notifications
You must be signed in to change notification settings - Fork 0
/
app2.js
55 lines (45 loc) · 1.43 KB
/
app2.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
const express = require('express');
const nunjucks = require('nunjucks');
const path = require('path');
const request=require('request');
const Datastore = require('nedb');
const PORT = 8080;
const app = express();
const fruits = [
{listname: 'apple', name: 'Apple', description: 'This is a Red Apple.'},
{listname: 'banana', name: 'Banana', description: 'This is a Yellow Banana. It is not a puffy Banana. :('},
{listname: 'pineapple', name: 'Pineapple', description: 'This is a Yellow Space Pineapple. It will take over the world and appear in Tom\'s Game.'},
];
app.use(express.static(path.join(__dirname, 'static')));
nunjucks.configure('templates', {
autoescape: true,
express: app
});
app.get('/', (req,res) => {
request
.get('http://localhost:8080/api/fruits?name='+req.query.Name)
.on('data', (data) => {
emojis = JSON.parse(data)
console.log("Rex Sneeze")
res.render("home.html", {'emojis': emojis})
})
});
app.get("/test", (req,res) => {
res.render("test.html", {name: req.query.FirstName, message: req.query.message, task: req.query.task})
})
app.get('/api/fruits', (req,res) =>{
let name = req.query.name;
let ffs = [];
fruits.forEach((fruit) => {
if (fruit.listname.indexOf(name) >= 0) {
ffs.push(fruit);
}
})
res.status(200).json(ffs);
})
app.get('*', function(req, res) {
res.redirect('/html/error.html');
});
const server = app.listen(PORT, () =>{
console.log('Server listing on', server.address());
});