admin管理员组

文章数量:1355679

How do I GET a JSON file with express.js? I want to be able to access it in my Mac terminal. I'm working on a college assignment that asks me to write an HTTP server that will act as a simple data store. It must respond to GET, PUT, POST, and DELETE requests. I must use express.js instead of fs for this app.

So far, in my root directory I have a server.js file and I have a subdirectory called lib that holds another subdirectory called notes. Notes is where the JSON files will live.

In my root directory, I have a server.js file. This is all I have so far:

'use strict'
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var notes = './lib/notes';

app.use(bodyParser.json());

app.get('/', function(req, res) {
  //
  //this is the part I need help with
  //
}

var port = process.env.PORT || 3000;
app.listen(port, function() {
  console.log('Server started on port ' + port;
});

Once I have this GET request working, from my Mac terminal I should be able to send a GET request and receive all JSON files inside the notes directory.

How do I GET a JSON file with express.js? I want to be able to access it in my Mac terminal. I'm working on a college assignment that asks me to write an HTTP server that will act as a simple data store. It must respond to GET, PUT, POST, and DELETE requests. I must use express.js instead of fs for this app.

So far, in my root directory I have a server.js file and I have a subdirectory called lib that holds another subdirectory called notes. Notes is where the JSON files will live.

In my root directory, I have a server.js file. This is all I have so far:

'use strict'
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var notes = './lib/notes';

app.use(bodyParser.json());

app.get('/', function(req, res) {
  //
  //this is the part I need help with
  //
}

var port = process.env.PORT || 3000;
app.listen(port, function() {
  console.log('Server started on port ' + port;
});

Once I have this GET request working, from my Mac terminal I should be able to send a GET request and receive all JSON files inside the notes directory.

Share Improve this question asked Jul 17, 2015 at 4:29 Farhad AhmedFarhad Ahmed 651 silver badge10 bronze badges 5
  • Does your notes folder contain the .json files? I must use express.js instead of fs for this app. What do you mean by fs? the fs module? – Rahat Mahbub Commented Jul 17, 2015 at 5:06
  • Correct. I meant the fs module. – Farhad Ahmed Commented Jul 17, 2015 at 5:08
  • Can your notes be stored somewhere else such as a database or the web? Because, using fs is the only way to read data from your hard drive. – Rahat Mahbub Commented Jul 17, 2015 at 5:10
  • We can't use a database. But Express allows us to use a res.send() function if we need the server to respond with data. – Farhad Ahmed Commented Jul 17, 2015 at 5:22
  • Yes... and also res.json. Anyway, in that case, you can use res.sendFile(notes +'file.json'); – Rahat Mahbub Commented Jul 17, 2015 at 5:26
Add a ment  | 

2 Answers 2

Reset to default 7

...from my Mac terminal I should be able to send a GET request and receive all JSON files inside the notes directory.

Provided you do not want to use fs module(well you dont need one either),

you can simply set a route for GET requests and send the json file in response with app.sendFile()

app.get('/',function(req,res){
    res.sendFile(path.normalize(__dirname + '/foo.json'))  
   //assuming your app.js and json file are at same level. 
   //You may change this to 'lib/notes/foo.json' to fit you case
})

path is a module that you would need to require().

__dirname is the directory that the currently executing script is in.

and finally foo.json is the file containing your json

{
    "name":"nalin",
    "origin":"stackoverflow"
}

Here's the plete code for app.js

var express = require('express');
var path = require('path');
var app  = express();


app.get('/',function(req,res){
    res.sendFile(path.normalize(__dirname + '/foo.json'))
})

app.listen(3000);

Which will help you run the node server with node app.js.

Finally you can access the json with by

  • visiting http://localhost:3000/ on your browser
  • by running curl mand on your mac terminal curl localhost:3000

Hope this helps.

You can serve your .json files as static:

app.use('/notes', express.static( notes ));

http://expressjs./starter/static-files.html

Or you can do it manually width path pattern:

app.get('/notes/:file', function(req, res) {
  fs.readFile(notes + "/" + req.params.file, function(err, data) {
    if(err) {
     res.status(404).send('Not found');
    } else {
      res.contentType(req.params.file);
      res.send(data);
    }   
    res.end();
  }); 
});

本文标签: javascriptexpressjs to GET json file in terminalStack Overflow