-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
55 lines (45 loc) · 1.59 KB
/
index.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
/*jshint esversion: 6*/
'use strict';
const express = require('express');
const app = express();
const reader = require('./server/reader.js');
const bodyParser = require('body-parser');
const debug = require('debug')('server');
const isProd = process.argv[2] === 'prod';
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
if (isProd) {
app.use(express.static(__dirname + '/public'));
} else {
// Mapping between files and their directories (for sourcemaps)
let stylesMap = new Map([
['about', 'about'],
['categories', 'category'],
['navBar', 'category/navBar'],
['recipePreview', 'category/recipePreview'],
['stars', 'common/starts'],
['mainMenu', 'mainMenu'],
['recipe', 'recipe'],
['search', 'search']
]);
app.use(express.static(__dirname + '/dist'));
app.use(express.static(__dirname + '/bower_components'));
app.use(express.static(__dirname + '/client'));
stylesMap.forEach((val, key) => {
app.get(`/source/${key}.styl`, (req, res) => {
res.sendFile(__dirname + `/client/components/${val}/${key}.styl`);
});
});
}
app.use(function(err, req, res, next) {
debug(`Server error: ${err.message}`);
res.writeHead(500, {'Content-Type': 'text/plain'});
res.end('Server error.\n');
});
app.get('/api/categories', reader.getCategories);
app.get('/api/recipe/:id', reader.getRecipe);
app.get('/api/tags', reader.getTags);
app.listen(8000);
debug('Server is running on port 8000');