node.jstypescriptnode-static

Getting the Server.serve status code using node-static in Typescript


I'm trying to write some of the node-static example code in Typescript and I'm getting the error

Property 'status' does not exist on type 'Error'

Here is my code with the error

import * as server from 'node-static';
import * as http from "http"

var fileServer = new server.Server('./images');

http.createServer(function (request, response) {
    request.addListener('end', function () {
        var callback: server.Callback;
        callback = function (e: Error) {
            if (e && (e.status === 404)) { // Error in this line 
                fileServer.serveFile('/not-found.html', 404, {}, request, response);
            }
        };
        fileServer.serve(request, response, callback)
    }).resume();
}).listen(8080);

Error has type string and is the stacktrace, so the reason for the typescript error is clear.
My question is how to get the status code.

The original JavaScript code is here


Solution

  • Looks like the @types for node-static is wrong. Link

    Especially because the Callback is a function that can also have a res object.

    You can always fallback to any