HMR
HMR

Reputation: 39280

nodejs express to serve static content on POST

I need to serve static content on POST requests but can't find how to do it.

Express has a .static method but it only seems to handle get requests. Not sure how I am going to get it to serve posts.

I have created json files that need to be served on a post so they can simulate endpoints without actually having the server code installed.

Tried something like this

var express = require('express');
var ws = express();
ws.use(express.static('../static'));
ws.get('*', four_oh_four);
ws.post(express.static('../static'));

Not sure how to get this to work, any help would be appreciated.

Upvotes: 0

Views: 784

Answers (1)

HMR
HMR

Reputation: 39280

Got it working the following way:

ws.post('*.json',dummyData );
function dummyData(paramRequest, paramResponse){
    var path = '../static'+paramRequest.url;
    fs.readFile(
        path,
        function (err, contents) {
            if (err) {
                send_failure(res, err);
                return;
            }
       contents = contents.toString('utf8');
            paramResponse.writeHead(200, { "Content-Type": "application/json" });
            paramResponse.end(contents);
        }
    );
}

Upvotes: 1

Related Questions