Skip to content

Commit 4b3b2a0

Browse files
committed
Merge pull request reactjs#84 from robatron/pr/js-fixes
JS: Fix undefined file reference when invoked from another directory
2 parents a52792b + 0444916 commit 4b3b2a0

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

server.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,26 @@ var express = require('express');
1616
var bodyParser = require('body-parser');
1717
var app = express();
1818

19+
var COMMENTS_FILE = path.join(__dirname, 'comments.json');
20+
1921
app.set('port', (process.env.PORT || 3000));
2022

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

2527
app.get('/api/comments', function(req, res) {
26-
fs.readFile('comments.json', function(err, data) {
28+
fs.readFile(COMMENTS_FILE, function(err, data) {
2729
res.setHeader('Cache-Control', 'no-cache');
2830
res.json(JSON.parse(data));
2931
});
3032
});
3133

3234
app.post('/api/comments', function(req, res) {
33-
fs.readFile('comments.json', function(err, data) {
35+
fs.readFile(COMMENTS_FILE, function(err, data) {
3436
var comments = JSON.parse(data);
3537
comments.push(req.body);
36-
fs.writeFile('comments.json', JSON.stringify(comments, null, 4), function(err) {
38+
fs.writeFile(COMMENTS_FILE, JSON.stringify(comments, null, 4), function(err) {
3739
res.setHeader('Cache-Control', 'no-cache');
3840
res.json(comments);
3941
});

0 commit comments

Comments
 (0)