Skip to content

Commit

Permalink
Merge pull request reactjs#84 from robatron/pr/js-fixes
Browse files Browse the repository at this point in the history
JS: Fix undefined file reference when invoked from another directory
  • Loading branch information
zpao committed Oct 9, 2015
2 parents a52792b + 0444916 commit 4b3b2a0
Showing 1 changed file with 5 additions and 3 deletions.
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');

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

0 comments on commit 4b3b2a0

Please sign in to comment.