When you are prototyping w. JS and JSON you need a webserver if you separate your data from your program.
So here is four tips 🙂
1. Use Python SimpleHTTPServer
Python will serve your files from the catalogue where you started the server
python –m SimpleHTTPServer
If you like to use NodeJS you have several options
2. You can use NodeJS “copy” of pythons SimpleHTTPServer
npm install simplehttpserver –g
Server listens port 8000. Navigate to http://localhost:8000 to view. You start the server by writing:
simplehttpserver [directory]
3. You can create your own simple server in NodeJS
## File server.js
“use strict”;
var express = require(‘express’);
var app = express();
app.use(express.static( __dirname ));
// Listen to port
app.listen(8000);
Then you can start you server and serve files from your project root
node server.js
4. You can serve a file up as rest api in NodeJS
## File server.js
“use strict”;
var express = require(‘express’);
var app = express();
app.use(‘/test’, function(req, res) {
res.json(require(“<yourfile.json>”));
});
// Listen to port
app.listen(8000);
Then you can start you server and serve files from your project root
node server.js
Now you can use your new REST interface by going to your browser and type
http://localhost:8000/test