Git Product home page Git Product logo

node-static-alias's Issues

Feature Request: include full request URL in parameters

It would be helpful to additionally have the full request URL (as exposed by req.url, like /foo/bar/baz.jpg?bam=1) available in the "Parameters" that are accessible for matching, both in the string (key=value) form and in the function form. Of course, it should also be available for the serve specification, like <% ... %>. I propose this new parameter be called "reqURL" to fit with "reqPath" and others.

I had a use-case where I wanted to serve a different file depending on if there was a ?SID=... query parameter in the URL. I had to work-around the lack of that information available for matching by pre-parsing req.url and manually overriding it, before calling serve(..).

Feature Request: support asynchrony for `match(..)` / `serve(..)` handler functions

Both match(..) and serve(..) expect an immediate return value.

But would you consider a minimal modification (not a breaking change, I don't think) where if either returns a promise, it's waited on to resolve before moving on?

For example:

// ..
{
   async match(params,req,res) {
      var result = await Router.check(params.reqUrl);
      if (url) return true;
      else return false;
   },
   async serve(params,req,res) {
      let reqBody = JSON.parse(await getStream(req));
      if (reqBody.addExtension) return params.reqPath + ".html";
      else return params.reqPath;
   }
}
// ..

In this contrived example, matching (true / false) is asynchronous because the router may be looking up a route from a database or other async persistence layer, and serving is asynchronous because it needs to read something from the request-body (retrieved asynchronously) to decide what filename to use.

Right now, I'm having to do this kind of stuff as pre-processing, doing the asynchronous waiting and adding properties to the req object, all before calling node-static-alias. It would be nice if that async logic could just be embedded into match(..) / serve(..).

I think all that needs to happen is that those handlers need to see if a promise is returned, and if so, simply call then(..) on it to wait. I don't think returning a promise would have been valid prior, so this doesn't seem like a breaking change.

"Cannot set headers..." error intermittently

I'm seeing this error show up in my log frequently but intermittently (like once every 5-10 requests, about once every 5 minutes or so):

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:471:11)
    at ServerResponse.writeHead (_http_server.js:231:21)
    at Server.finish (/tmp/site/node_modules/node-static/lib/node-static.js:125:17)
    at finish (/tmp/site/node_modules/node-static/lib/node-static.js:170:14)
    at /tmp/site/node_modules/node-static/lib/node-static.js:337:13
    at streamFile (/tmp/site/node_modules/node-static/lib/node-static.js:382:13)
    at ReadStream.<anonymous> (/tmp/site/node_modules/node-static/lib/node-static.js:375:17)
    at ReadStream.emit (events.js:182:13)
    at fs.close (fs.js:2161:14)
    at FSReqWrap.oncomplete (fs.js:152:20)

I have logged out what URL is being requested that's causing this error, but it's a normal URL that when I try it, it works fine for me, and does not cause this error in the log. So it has to be something other than the URL (not sure, maybe some other headers?).

Anyway, my code is very basic... it looks like this:

var nodeStaticAlias = require("node-static-alias");
var staticServer = new nodeStaticAlias.Server(path.join(__dirname,"static"),{
        serverInfo: "my-server",
        cache: 86400,
        gzip: true,
        alias: [
                {
                        match: /[^]/,
                        serve: "index.html",
                },
        ],
});

And in my HTTP request handler:

res.setHeader("Strict-Transport-Security","max-age=" + 1E9 + "; includeSubdomains");

// unconditional, permanent HTTPS redirect
if (req.headers["x-forwarded-proto"] !== "https") {
    res.writeHead(301,{
        "Cache-Control": "public, max-age=31536000",
        Expires: new Date(Date.now() + 31536000000).toUTCString(),
        Location: `https://${req.headers["host"]}${req.url}`
    });
    res.end();
}
else if (["GET","HEAD"].includes(req.method)) {
    staticServer.serve(req,res);
}
else {
    res.writeHead(404);
    res.end();
}

Any thoughts on how I can track down what's causing this and fix it?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.