1

I have an SPA that requests data at a specific REST url, say, "/things".

In my production server, this request is handled by a query to a database and the construction of JSON to represent the result. Nothing fancy.

How would I configure lite-server (or BrowserSync) to respond to a GET at "/things" with the content of "/testdata/test-things.json"?

I apologize if this question has been answered (it feels like a very rudimentary use case), but neither the docs for lite-server nor for BrowserSync answer the question, and the Google has failed me.

1 Answers1

1

The answer, I've determined, is "You can't do this with lite-server". My alternative is to use Express, which provides an API to define routing rules that do exactly what I need. It's a little hacky (e.g., response.sendFile(...) doesn't support relative paths, hence the path.join(...) quackery below), but I'll make it work.

My tests will be run against an instance of Node bootstrapping with a script like this one:

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

var app = express();

app.use(express.static('.'));

app.get('/',
    function (req, res) {
        debug("Root URL requested.");
        res.sendFile(path.join(__dirname, 'index.html'));
    });

app.get('/test',
    function (req, res) {
        debug("GET on /test")
        res.send('Hello World');
    });

var port = process.env.PORT || 3000;
debug("Using port ", port);
var server = app.listen(port,
    function() {
        var host = server.address().address;
        var port = server.address().port;
        console.log('Listening at http://%s:%s', host, port);
    });
}

I've updated package.json like so:

  "main": "nodeapp.js",
  "scripts": {
    "cc": "concurrently \"npm -v\"  \"npm run tsc:w\" ",
    "start": "concurrently \"npm run tsc -v\" \"npm run tsc:w\" \"node nodeapp.js\" ",
    ...