This repository has been archived by the owner on May 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
72 lines (57 loc) · 2.57 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
// Load modules.
// It is good practice to put includes and modules at the top of your files.
var express = require('express');
var serveStatic = require('serve-static');
// Create our express app.
var app = express();
// Serve statically every file in the public directory.
// These files will be available to our browser.
app.use(serveStatic(__dirname + '/public'));
// We also need jQuery on the client-side.
app.use(serveStatic(__dirname + '/node_modules/jquery/dist'));
var resources = [
{
name: 'JavaScript Introduction',
description: 'This chapter introduces JavaScript and discusses some of its fundamental concepts.',
url: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Introduction'
},
{
name: 'Web technology for developers',
description: 'The open nature of the World Wide Web presents incredible opportunities for people who want to create websites or online applications. To take full advantage of the Web\'s capabilities, you need to know how to use them. Explore the links below to learn more about various Web technologies.',
url: 'https://developer.mozilla.org/en-US/docs/Web'
},
{
name: 'Learn Javascript',
description: 'This book will teach you the basics of programming and Javascript. Whether you are an experienced programmer or not, this book is intended for everyone who wishes to learn the JavaScript programming language.',
url: 'https://www.gitbook.com/book/gitbookio/javascript/details'
}
];
app.get('/search', function(req, res, next) {
// The code here executes any time your browser makes a request to http://localhost:3000/search
console.log('GET /search');
var results = getMatchingResources(req.query.q);
res.json(results).status(200).end();
});
// This function searches the array of resources and returns the matching ones.
var getMatchingResources = function(q) {
var results = [];
// Loop over each resource.
resources.forEach(function(resource) {
// Get the name and description for this resource as lowercase.
// This allows case-insensitive searching.
var name = resource.name.toLowerCase();
var description = resource.description.toLowerCase();
// Can we find the search text (q) in either the name or description?
var isMatch = name.indexOf(q) !== -1 || description.indexOf(q) !== -1;
if (isMatch) {
// This resource matches our search query.
// Add it to the results array.
results.push(resource);
}
});
return results;
};
// This tells our express application to listen for local requests on port 3000.
app.listen(3000, function() {
console.log('Server started and listening at localhost:3000');
});