Skip to content
This repository was archived by the owner on Jul 19, 2019. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
JS: Make comments file reference relative to server file
  • Loading branch information
robatron committed Oct 9, 2015
commit 0444916c04c39bb27c1944f0e09219859ff5740c
8 changes: 5 additions & 3 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,26 @@ var express = require('express');
var bodyParser = require('body-parser');
var app = express();

var COMMENTS_FILE = path.join(__dirname, 'comments.json');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Matches the script-relative path pattern used to reference the project's 'public' directory on line 23.


app.set('port', (process.env.PORT || 3000));

app.use('/', express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

app.get('/api/comments', function(req, res) {
fs.readFile('comments.json', function(err, data) {
fs.readFile(COMMENTS_FILE, function(err, data) {
res.setHeader('Cache-Control', 'no-cache');
res.json(JSON.parse(data));
});
});

app.post('/api/comments', function(req, res) {
fs.readFile('comments.json', function(err, data) {
fs.readFile(COMMENTS_FILE, function(err, data) {
var comments = JSON.parse(data);
comments.push(req.body);
fs.writeFile('comments.json', JSON.stringify(comments, null, 4), function(err) {
fs.writeFile(COMMENTS_FILE, JSON.stringify(comments, null, 4), function(err) {
res.setHeader('Cache-Control', 'no-cache');
res.json(comments);
});
Expand Down