Giter Club home page Giter Club logo

serve-handler's Introduction

serve-handler

Tests codecov install size

This package represents the core of serve. It can be plugged into any HTTP server and is responsible for routing requests and handling responses.

In order to customize the default behaviour, you can also pass custom routing rules, provide your own methods for interacting with the file system and much more.

Usage

Get started by installing the package using yarn:

yarn add serve-handler

You can also use npm instead, if you'd like:

npm install serve-handler

Next, add it to your HTTP server. Here's an example using micro:

const handler = require('serve-handler');

module.exports = async (request, response) => {
  await handler(request, response);
};

That's it! πŸŽ‰

Options

If you want to customize the package's default behaviour, you can use the third argument of the function call to pass any of the configuration options listed below. Here's an example:

await handler(request, response, {
  cleanUrls: true
});

You can use any of the following options:

Property Description
public Set a sub directory to be served
cleanUrls Have the .html extension stripped from paths
rewrites Rewrite paths to different paths
redirects Forward paths to different paths or external URLs
headers Set custom headers for specific paths
directoryListing Disable directory listing or restrict it to certain paths
unlisted Exclude paths from the directory listing
trailingSlash Remove or add trailing slashes to all paths
renderSingle If a directory only contains one file, render it
symlinks Resolve symlinks instead of rendering a 404 error
etag Calculate a strong ETag response header, instead of Last-Modified

public (String)

By default, the current working directory will be served. If you only want to serve a specific path, you can use this options to pass an absolute path or a custom directory to be served relative to the current working directory.

For example, if serving a Jekyll app, it would look like this:

{
  "public": "_site"
}

Using absolute path:

{
  "public": "/path/to/your/_site"
}

NOTE: The path cannot contain globs or regular expressions.

cleanUrls (Boolean|Array)

By default, all .html files can be accessed without their extension.

If one of these extensions is used at the end of a filename, it will automatically perform a redirect with status code 301 to the same path, but with the extension dropped.

You can disable the feature like follows:

{
  "cleanUrls": false
}

However, you can also restrict it to certain paths:

{
  "cleanUrls": [
    "/app/**",
    "/!components/**"
  ]
}

NOTE: The paths can only contain globs that are matched using minimatch.

rewrites (Array)

If you want your visitors to receive a response under a certain path, but actually serve a completely different one behind the curtains, this option is what you need.

It's perfect for single page applications (SPAs), for example:

{
  "rewrites": [
    { "source": "app/**", "destination": "/index.html" },
    { "source": "projects/*/edit", "destination": "/edit-project.html" }
  ]
}

You can also use so-called "routing segments" as follows:

{
  "rewrites": [
    { "source": "/projects/:id/edit", "destination": "/edit-project-:id.html" },
  ]
}

Now, if a visitor accesses /projects/123/edit, it will respond with the file /edit-project-123.html.

NOTE: The paths can contain globs (matched using minimatch) or regular expressions (match using path-to-regexp).

redirects (Array)

In order to redirect visits to a certain path to a different one (or even an external URL), you can use this option:

{
  "redirects": [
    { "source": "/from", "destination": "/to" },
    { "source": "/old-pages/**", "destination": "/home" }
  ]
}

By default, all of them are performed with the status code 301, but this behavior can be adjusted by setting the type property directly on the object (see below).

Just like with rewrites, you can also use routing segments:

{
  "redirects": [
    { "source": "/old-docs/:id", "destination": "/new-docs/:id" },
    { "source": "/old", "destination": "/new", "type": 302 }
  ]
}

In the example above, /old-docs/12 would be forwarded to /new-docs/12 with status code 301. In addition /old would be forwarded to /new with status code 302.

NOTE: The paths can contain globs (matched using minimatch) or regular expressions (match using path-to-regexp).

headers (Array)

Allows you to set custom headers (and overwrite the default ones) for certain paths:

{
  "headers": [
    {
      "source" : "**/*.@(jpg|jpeg|gif|png)",
      "headers" : [{
        "key" : "Cache-Control",
        "value" : "max-age=7200"
      }]
    }, {
      "source" : "404.html",
      "headers" : [{
        "key" : "Cache-Control",
        "value" : "max-age=300"
      }]
    }
  ]
}

If you define the ETag header for a path, the handler will automatically reply with status code 304 for that path if a request comes in with a matching If-None-Match header.

If you set a header value to null it removes any previous defined header with the same key.

NOTE: The paths can only contain globs that are matched using minimatch.

directoryListing (Boolean|Array)

For paths are not files, but directories, the package will automatically render a good-looking list of all the files and directories contained inside that directory.

If you'd like to disable this for all paths, set this option to false. Furthermore, you can also restrict it to certain directory paths if you want:

{
  "directoryListing": [
    "/assets/**",
    "/!assets/private"
  ]
}

NOTE: The paths can only contain globs that are matched using minimatch.

unlisted (Array)

In certain cases, you might not want a file or directory to appear in the directory listing. In these situations, there are two ways of solving this problem.

Either you disable the directory listing entirely (like shown here), or you exclude certain paths from those listings by adding them all to this config property.

{
  "unlisted": [
    ".DS_Store",
    ".git"
  ]
}

The items shown above are excluded from the directory listing by default.

NOTE: The paths can only contain globs that are matched using minimatch.

trailingSlash (Boolean)

By default, the package will try to make assumptions for when to add trailing slashes to your URLs or not. If you want to remove them, set this property to false and true if you want to force them on all URLs:

{
  "trailingSlash": true
}

With the above config, a request to /test would now result in a 301 redirect to /test/.

renderSingle (Boolean)

Sometimes you might want to have a directory path actually render a file, if the directory only contains one. This is only useful for any files that are not .html files (for those, cleanUrls is faster).

This is disabled by default and can be enabled like this:

{
  "renderSingle": true
}

After that, if you access your directory /test (for example), you will see an image being rendered if the directory contains a single image file.

symlinks (Boolean)

For security purposes, symlinks are disabled by default. If serve-handler encounters a symlink, it will treat it as if it doesn't exist in the first place. In turn, a 404 error is rendered for that path.

However, this behavior can easily be adjusted:

{
  "symlinks": true
}

Once this property is set as shown above, all symlinks will automatically be resolved to their targets.

etag (Boolean)

HTTP response headers will contain a strong ETag response header, instead of a Last-Modified header. Opt-in because calculating the hash value may be computationally expensive for large files.

Sending an ETag header is disabled by default and can be enabled like this:

{
  "etag": true
}

Error templates

The handler will automatically determine the right error format if one occurs and then sends it to the client in that format.

Furthermore, this allows you to not just specifiy an error template for 404 errors, but also for all other errors that can occur (e.g. 400 or 500).

Just add a <status-code>.html file to the root directory and you're good.

Middleware

If you want to replace the methods the package is using for interacting with the file system and sending responses, you can pass them as the fourth argument to the function call.

These are the methods used by the package (they can all return a Promise or be asynchronous):

await handler(request, response, undefined, {
  lstat(path) {},
  realpath(path) {},
  createReadStream(path, config) {}
  readdir(path) {},
  sendError(absolutePath, response, acceptsJSON, root, handlers, config, error) {}
});

NOTE: It's important that – for native methods like createReadStream – all arguments are passed on to the native call.

Author

Leo Lamprecht (@notquiteleo) - Vercel

serve-handler's People

Contributors

andybitz avatar andyburke avatar axllind avatar balupton avatar compulim avatar dependabot[bot] avatar igorklopov avatar joeldenning avatar kachkaev avatar leo avatar manovotny avatar mischnic avatar mungell avatar pd4d10 avatar quocnguyen avatar remy avatar terrencewwong avatar thasophearak avatar timothyis avatar tootallnate avatar wellingguzman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

serve-handler's Issues

Negating a previous capture with "not" in rewrites

It could just be that I am misunderstanding how this works. But I've tried every combination I can think of. My goal is to direct all routes without /dp to index, and everything with /dp to it's html file. Is this possible?

      rewrites: [
        { source: '!/dp', destination: '/index.html' },
        { source: '/dp/:id', destination: '/dp/:id.html' }
      ],

Edit: This is NOT related to #77 ... I've rolled back to 5.0.7 and tested on that version as well.

When both folder and file are present file is prefered

With the following directory structure:

images
 |- image1.png
 |- image2.png
 |- image3.png
images.html

images.html is preferred over images when going to /images.

I'm not sure if this is the standard behavior of other web servers.

Example of how it should probably be handled:

/images => images.html and /images/ => images directory index

fail to set response headers

const handler = require('serve-handler');
const http = require('http');
const path = require('path');

const server = http.createServer(async(req, res) => {
  // res.setHeader('Content-Encoding', 'gzip')
  await handler(req, res, {
    public: path.resolve(__dirname, './dist'),
    headers: [
      {
        "source" : path.resolve('./dist/js/chunk-elementUI.1d709726.js.gz'),
        "headers" : [{
          "key" : "Content-Encoding",
          "value" : "gzip"
        }]
      }
    ]
  });
});

console.log(server)
server.listen(3000, () => {
  console.log('Listening on http://localhost:3000');
});

Internal error on paths like '/static/a/abc' if 'a' is a file

Problem

Accessing a path like static/a/abc will throw an error and respond with status code 500 if a is a file. This is because this generates an ENOTDIR error, instead of ENOENT.

We discovered this since we serve images at static/*.jpg and paths like static/*.jpg/abc returned 500 instead of 404.

Expectation

Should respond with status code 404 and show the 404 page.

Solution

In the code you check forif (err.code !== 'ENOENT'). Adding && err.code !== 'ENOTDIR' for example here makes it correctly respond with 404 instead of 500:

https://github.com/zeit/serve-handler/blob/d1368bf5ece565b27850ceaab2758a5ae0a4579d/src/index.js#L611

process.cwd not always point to main script directory

I do have a question, I don't know if this is a bug or an expected behavior.

When trying to run a http server that uses serve-handler works as expected as long as the process.cwd() returns the same path of the script, instead of /.

Setup:

await handler(request, response, {
  public: "www"
});

Running script

The example below works as expected and the public path is set correctly.

https://github.com/zeit/serve-handler/blob/master/src/index.js#L508

Example 1:

# process.cwd() === /path/to
# public path === /path/to/www
$ node /path/to/index.js

I am using forever and it seems forever current working directory is set to /.

Example 2:

# process.cwd() === /
# public path === /www
$ forever start /path/to/index.js

Result

The serve-handler module tries to find files in the /www directory instead of the correct directory /path/to/www.

Using absolute path doesn't fix it, as it will create an incorrect path for Example 1.

Security Issue

Hi,
I've discovered a potential security flaw. Would it be okay if I open a PR for the fix or would you like it to be disclosed privately first?

Add HTTP Auth

It would be nice if we could have and HTTP auth flag which we can define in the now.json. There are a few static pages that I have that I would like to hide behind a basic auth.

I believe the feature existed in previous versions of server but it is gone now.

cc @leo

Can I just use serve like a static server without file listing?

Here i'm using serve like a static file server, I store files in it and visit from my program. But here comes a problem that if someone see my link such as http://example.com/1.jpeg, they can cut off the filename and visit http://example.com, then they will get a whole list of my files. Sometimes this will bring me some trouble.

Can I have an option to hide files from listing by visiting the root domain? I mean, when people open my domain, they will get nothing, but still they can get my files by direct link.

Using glob with capture for rewrites

Here's the closest example to what I want to do for rewrites that I found:

{
  "rewrites": [
    { "source": "/projects/:id/edit", "destination": "/edit-project-:id.html" },
  ]
}

What I really want to do is have

  • /pages/** all handled by /index.html
  • /tags/** all handled by /index.html

But I don't want to rewrite the url. I just want to have all requests be handled by my react app, the entry point for which is index.html.

What I find now is that if I go directly to a page (let's say /pages/foo), or do a forced refresh of that page, react doesn't load/route properly and returns a 404.

So what rules do I need to have all requests under the specific paths /pages/** and /tags/** all handled by /index.html but without changing the URL?

support runtime environment variables

I want to serve a Create-React-App production build at different stages.

So I want to set all REACT_APP_ environment variables during runtime, not at build time.

In order to make this work I would like to utilize a tool similar to react-env, which creates a env.js file, which will hold all environment variables like:

window._env = { REACT_ENV_FOO: 'BAR' };

This can be archived by using a middleware

Custom directory index template or add additional html

My use-case: I want to deploy a folder of photos, and have the directory page use a lightbox to view the photos. Would be really awesome to be able to use serve-handler directly and just supply a custom template or perhaps a way to just append some html to the output.

I tried to look for a similar issue but didn't see anything. If that's something that other people are interested in, i'd be happy to PR it.

Wrong redirect that cause wrong relative links interpolation on html files

it redirects urls like:
http://localhost:8000/abc/def/index.html to http://localhost:8000/abc/def
if index.html contains links like <a href="file.html">File</a> browser resolves that links like:
http://localhost:8000/abc/file.html instead of http://localhost:8000/abc/def/file.html

actual behaviour:
redirects http://localhost:8000/abc/def/index.html to http://localhost:8000/abc/def
expected behaviour:
redirects http://localhost:8000/abc/def/index.html to http://localhost:8000/abc/def/

I compared it to for example SimpleHTTPServer from python, and it works correctly.

Multiple `Set-Cookie` headers?

Is there any way I can set multiple Set-Cookie headers? Currently if I do the following, only the last item in the array, b=b, is set:

handler(request, response, {
  public: "public",
  headers: [
    {
      source: "**",
      headers: [
        {
          key: "Set-Cookie",
          value: `a=a`
        },
        {
          key: "Set-Cookie",
          value: `b=b`
        }
      ]
    }
  ]
});

Omitting files from trailingSlash redirect behaviour

Hi,

I'm using the trailingSlash: true option to redirect all my pages from /page to /page/. However this has the unintended consequence of also redirecting /image.jpeg to /image.jpeg/.

After having a look through the ZEIT now spectrum community, someone suggested:

routes [{ 
    "src": "/(((?!\\?|#|\\.|_next).)*((?!\\?|/|#|\\.).))", 
    "status": 301,
    "headers": { "Location": "/$1/" }
 }]

Eg. redirect all paths to trailing slash, unless it's a file (or the _next) folder.

Am I missing something here or isn't this the intended behaviour of trailingSlash?

Thanks in advance

[Feature Request] Custom Port

I'd love to be able to customize the port the data is served on via the serve.json file. I'll submit a PR if need be.

Support for rewriting to external URLs

Would be great to have proxy support for external APIs in this microservice world. This would also alleviate the pain of using CORs. Also in relation to issue #28

{
  "proxy": [
    { "path": "api/**", "destination": "https://xxx.api/" }
  ]
}

Redirect all routes to a single page with some exception

In a SPA settings, I'm prerendering the some landing pages for SEO and perf. But I can't figure out the corresponding rewrites rules.

So far, I've tried:

{
	"rewrites": [
		{ "source": "", "destination": "prerender/landing.html" },
		{
			"source": "/landing-2",
			"destination": "prerender/landing-2.html"
		},
		{ "source": "**", "destination": "index.html" }
	],
...
}

This doesn't work because apparently, all the rewrites rules are applied to the path (instead of stopping after the first match).

The problem is that I can't seem to find a way to negate the two prerendered path in the last rule, using minimatch.

What would be the proper way to handle this use case ? Is it even possible ?

Thanks!

Custom handler for not found urls?

With serve-static it'd looks like:

const serveStatic = require('serve-static')

const serve = serveStatic('build');

serve(req, res, () => {
    // if we're there, the file was not found
    res.end('...')
  });

not sure this lib can do this?

Backreferences in rewrites?

I'm trying to rewrite everything under /admin, e.g.

  • /admin => /
  • /admin/index.html => /index.html
  • /admin/static/style.css => /static/style.css
  • /admin/static/main.js => /static/main.js

...and so on.

Can this be accomplished using rewrites in serve.json without specificying every possible path?

I tried the following but no luck:

{
  "rewrites": [{
    "source": "/admin/(.*)",
    "destination": "/$1"
  }]
}

Configuration breaks with long header names

The most recent version breaks when provided with header names longer than 32 characters. For example, we are using Content-Security-Policy-Report-Only and serve will not start:

INFO: Discovered configuration in `serve.json`
ERROR: The configuration you provided is wrong:
should NOT be longer than 32 characters
{"limit":32}

Error: Cannot find module './glob-slash'

Having trouble using serve-handler in my Express app. Developing on Mac. Have tried deleting node_modules and installing again.

Running npm i glob-slash doesn't seem to fix the issue either.

Specify full address for rewrite destination?

I am launching serve with a serve.json file. I have a rewrite like this:

{
  "source": "/foo/api/:rest",
  "destination": "http:/xxx.xx.x.xx:8080/foo/api/:rest"
}

But this causes an error when I launch serve:

(node:14574) UnhandledPromiseRejectionWarning: TypeError: Expected "8080" to be a string
    at /Users/sdavico/Development/installer/webapp/node_modules/serve-handler/node_modules/path-to-regexp/index.js:194:13
    at toTarget (/Users/sdavico/Development/installer/webapp/node_modules/serve-handler/src/index.js:68:9)
    at applyRewrites (/Users/sdavico/Development/installer/webapp/node_modules/serve-handler/src/index.js:85:18)
    at module.exports (/Users/sdavico/Development/installer/webapp/node_modules/serve-handler/src/index.js:547:24)
    at Server.http.createServer (/Users/sdavico/Development/installer/webapp/node_modules/serve/bin/serve.js:147:58)
    at Server.emit (events.js:160:13)
    at parserOnIncoming (_http_server.js:618:12)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:115:23)

This is due to using path-to-regexp to process the destination. Could we add support for full URLs?

Support for automating redirect rules with Google Spreadsheets API

Since I am trying to create 500+ redirect rules I was thinking that there must be an easier way than doing this

redirects: [
   { source: '/partners*', destination: '/company#partners', type: 301 },
   ...
]

500+ times. The SEO expert I am working with on this project created a Google Spreadsheet which holds all the old domains and where they should redirect to. I thought of just looping over that file by creating an API from the Google spreadsheet, call it in my index.js and generate those 500+ redirects automatically.

Unfortunately it seems that I can't really import or fetch anything in the index.js which should be designed as describes here: https://github.com/zeit/serve-handler#usage.

Does anybody know why this is different from all the other .js files in my React project and how I can solve this? Thanks!

How to test local HTTPS?

Hi folks,

I would like to test my local build. I have already a local test CA and private key and certificate.
I use mkcert for that.

Are there any options for this?

Kind regards,
Remzi

Windows error in the root directory

Due to the direct use of fs.lstat, in acquiring similar D: \ when the file, Windows system folder "System Volume Information" method would result in an exception.

1.png

Perhaps you can first use fs.readdir list all the files, then use fs.lstat to get detailed information and filter list.

So even if some files wrong, it will not affect the normal display other files.

Repeated segments should be supported by passing array to pathToRegExp.compile

Context

  • Serving react app under /app prefix e.g. example.com/app/products
  • Installed serve and customized with serve.json
  • Used "homepage":"/app" in package.json according to CRA docs
  • Asset path is correctly inferred and generated but serve is not serving asset files correctly under build/static/...

serve.json used for above:

{
  "public": "build",
  "rewrites": [
    { "source": "/app/static/:segment*", "destination": "/static/:segment*" },
    { "source": "/app", "destination": "/index.html"}.
    { "source": "/app/**", "destination": "/index.html"}
  ]
}

After some debugging, I identified the issue to be in toTarget. We are not passing in array of parameters when we are compiling the valid path https://github.com/zeit/serve-handler/blob/e7c4e42a63e9cd59b43256bb4ca04a22a5f77994/src/index.js#L79-L86

We should be passing array according to the docs(v2.2.1 that we are using). If not it treats the whole string as a single segment value and encodes / .

Debug logs:

// applyRewrites
relativePath: "/app/static/css/2.c7bb94bc.chunk.css" //argument
rewrittenPath: "/app/%2Fcss%2F2.c7bb94bc.chunk.css" // return value

// toTarget
props: {
  "segment": "/css/2.c7bb94bc.chunk.css"
}
normalizedDest: "/static/:segment*"
toPath(props): "/static/%2Fcss%2F2.c7bb94bc.chunk.css"

This should be easily fixed by doing a split by / when we are setting the props i.e. props[name] = results[index + 1].split("/");

This will fix repeated segment for redirects as well.

Documents should also be updated with instructions on how to use repeated segment. (I figured out during debugging that destination needs * as well else the compile function will complain that the url built does not match the destination pattern πŸ˜…)

Not sure if my proposed solution is correct but it works locally for me so far. I am happy to work on this with tests and docs for repeated segment

TypeScript typings

Hi there,

I couldn't find any TypeScript typings and wrote these:

declare module 'serve-handler' {
  import { IncomingMessage, ServerResponse } from 'http'

  export interface IHeader {
    key: string
    value: string
  }

  export interface IRedirect {
    source: string
    destination: string
  }

  export interface IServeHandlerOptions {
    public?: string
    cleanUrls?: boolean | string[],
    rewrites?: IRedirect[],
    redirects?: IRedirect[],
    headers?: IHeader[],
    directoryListing?: boolean | string[],
    unlisted?: string[],
    trailingSlash?: boolean,
    renderSingle?: boolean,
    symlinks?: boolean
  }

  export default function (request: IncomingMessage, response: ServerResponse, options?: IServeHandlerOptions): void
}

Hope these will help others using this library with TypeScript!

url parameter removed when cleanUrls: true

when the config cleanUrls is set to true (by default)
and the requested URL is a file name with an extension
and has some parameter in it ex:

/index_2.html?foo&bar=1

the server rewrite the url to

/index_2

and remove the extension AND the url parameters

those URL on the other hand works as intended :

/index_2?foo&bar=1
/?foo&bar=1

/folderName?foo&bar=1

/folderName/?foo&bar=1

question :
is it as intended ?

If the response is "yes, you should set cleanUrls to false" then the README should be mentioning those cases

but I think the cleanUrls setting should set urls like :

/index.html?foo&bar=1 
to
/?foo&bar=1 

it would be more useful

Rewrite not working in 5.0.8

After updating serve-handler from 5.0.7 to 5.0.8 my rewrites don't seem to work. My client side routes just show 404. This also happens in https://github.com/zeit/serve with the --single option.

Minimal code:

const http = require("http");
const handler = require("serve-handler");

const handlerOptions = {
    public: "./dist",
    rewrites: [{ source: "**", destination: "/index.html" }],
};

const requestListener = async (request, response) => {
    await handler(request, response, handlerOptions);
};

http.createServer(requestListener).listen(8080);

console.log(`Started webserver http://localhost:8080/`);

Using Windows 10, NodeJS 11.9.0

Options not supported on Windows

Functionality that requires glob and/or file path matching does not work on Windows systems. The unit tests are failing, which support this. Here is a sample:

image

I've experienced this not working with:

  • Headers
  • Rewrites
  • Redirects

Security issue

Hi,
Is [email protected] this still your security email address? Have reported a potential security flaw few weeks ago and it would be nice to hear back on it from you.
Thanks

Rewriting URL to remove prefix from request

Problem-Description

I get my requests to the following URL /some/frontend/static/js/main.js.

My build folder to be served has the following structure

build
|_ static
   |_ js
      |_ main.js

Therefore serve should translate the url /some/frontend/static/js/main.js into the path static/js/main.js.

As far as I know a rewrite would be the way to go. However it doesnt work out for me, instead I found out serve creates a wrong path for me.

serve.json

{
  "rewrites": [{ "source": "/some/frontend/:path*", "destination": "/:path" }]
}

The serve handler translates the path into /static%2Fjs%2Fmain.5a829761.js. So he is escaping it.
This comes from line 70 in the index.js file, where the translation takes place.

Is there a way to define my rewrite route correctly?

Security vulnerability on npm audit

Running npm audit found a vulnerability as a deep dependency on my project:

                       === npm audit security report ===

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                                Manual Review                                 β”‚
β”‚            Some vulnerabilities require your attention to resolve            β”‚
β”‚                                                                              β”‚
β”‚         Visit https://go.npm.me/audit-guide for additional guidance          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Low           β”‚ Prototype Pollution                                          β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Package       β”‚ lodash                                                       β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Patched in    β”‚ >=4.17.5                                                     β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Dependency of β”‚ serve [dev]                                                  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Path          β”‚ serve > serve-handler > glob-slasher > toxic > lodash        β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ More info     β”‚ https://nodesecurity.io/advisories/577                       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

You should update glob-slasher version.
It has already been fixed here and here

Redirect with source "file" path not working correctly to destination

Hello
Example: Say I have this redirect

{
    "redirects": [
        { "source": "/some/old-path.html", "destination": "/new-path", "type": 301 }
    ]
}

The result is

HTTP/1.1 301 Moved Permanently
Location: /some/old-path
Date: Fri, 31 Jan 2020 03:39:03 GMT
Connection: keep-alive

instead expected result

HTTP/1.1 301 Moved Permanently
Location: /new-path
Date: Fri, 31 Jan 2020 03:39:03 GMT
Connection: keep-alive

serve version 11.3.0

serve-handler breaks relative paths in files named index.html

If you have an index.html in a subdirectory default behavior strips out the index.html part of the url which breaks relative paths. public/foo/index.html becomes public/foo.

This causes an issue when you have a relative script in public/foo/index.html referencing <script src="main.js"></script>. Instead of looking for public/foo/main.js default browser behavior is now to look for public/main.js.

Setting cleanUrls to true would fix this issue, except it causes the public/foo/index.html link to redirect to the directory listing for public/foo causing public/foo/index.html to be inaccessible.

If this is intended default behavior it should probably be documented, if not it would be great to have an option to serve index.html files.

Generate ETag

Hi, we used the serve since 6.5.3 version with -c option which automatically generated ETag in the response. However in recent versions I see that this option is no longer supported.

Also I see that now it is possible to define ETag header in serve.json.

Is there any way to generate it automatically for each server run?

Can't figure out to configure to serve a virtual path

Hi,
I would like the server listen to a virtual path / base-path like this - localhost:/.
I used the rewrite property but I didnt found a way that serve the index.html file when accesing without postfix while serving the other files.
My configuration:
{ "rewrites": [ { "source": "/virtual-path", "destination": "/index.html" }, { "source": "/virtual-path/:rest", "destination": "/:rest" } ] }

Please help,
Thanks

Serve 11 cannot set symlinks to true in config.json

serve@^11 currently errors out when setting symlinks: true:

root@c3ee02fdad56:/app# serve --version
11.0.0
root@c3ee02fdad56:/app# serve -p 8000 -c ./serve.json
INFO: Discovered configuration in `./serve.json`
ERROR: The configuration you provided is wrong:
should NOT have additional properties
{"additionalProperty":"symlinks"}

Redirect http to https?

Is it possible to do redirect from http to https using redirects? If so, how?

Something like this

redirects: [{
            source: "http://**",
            destination: 'https://**',
            type: 301
        }]

but source is phrased incorrectly or the protocol part cannot be used.

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.