- Notifications
You must be signed in to change notification settings - Fork1
HTTPS server running on localhost
License
homebase-garage/localhttps
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
I'm looking for maintainers and contributors!Learn more. |
---|
Run an express server on localhost with HTTP2 and SSL. Serve static files or import as module in your project.
https-localhost is a lightweight tool for serving static content on SSL thanks to locally-trusted development certificates.
It works with MacOS, Linux and Windows, on Chrome and Firefox, and requires you no configuration.
nss/certutils are needed for Firefox and Chrome.
brew install nss
sudo apt install libnss3-tools -or-sudo yum install nss-tools -or-sudo pacman -S nss
npm i -g --only=prod https-localhost
serve~/myproj
sudo
may be necessary.- If a static path is not provided the current directory content will be served.
- You can change theport setting the
PORT
environmental variable:PORT=4433 serve ~/myproj
. Specifying port number will also prevent http to https redirect. - You can change thehost setting the
HOST
environmental variable:HOST=example.com serve ~/myproj
.
If you don't have Node.js installed just use a packaged version! Download it from therelease page.
# Linux./https-localhost-linux~/myproj# MacOS./https-localhost-macos~/myproj# Windows./https-localhost-win.exe C:\User\me\myproj
Tip 1: on Windows just drag the folder on the executable to serve it.
Tip 2: on all platform put the executable on the folder you want to serve and double-click it.
Install as a dependency:
npm i -s https-localhost
Then put in yourindex.js
file:
constapp=require("https-localhost")()// app is an express app, do what you usually do with expressapp.listen(port)
- If theport number is not provided, it will listen on 443.
- Toredirect the http traffic to https use
app.redirect()
. - You can servestatic files with
app.serve(path)
. - You can create a certificate for additional domains with
require("https-localhost")("mydomain.com")
Tip: consider installing it as a dev dependency: this is not a production tool!npm i --save-dev https-localhost
consthttpsLocalhost=require("https-localhost")()// const app = ...// const port = 443constcerts=awaithttpsLocalhost.getCerts()constserver=https.createServer(certs,app).listen(port)
This tool has a production version that activatesHTTP/2,compression andminify.
NODE_ENV=production serve~/myproj
I decide to not activate it by default since it is usually an unwanted behaviour for localhost testing, but sometimes it could be userful, e.g. to test Progressive Web Application or more ingeneral the website performances.
IMPORTANT: the fact that there is a production enviornment doesn't mean that this tool is suitable for production. It's intended to be used only for local testing.
Serving static content on localhost in a trusted SSL connection is not so simple.
It requires to manually generate and trust certificates, with complicate commands and many manual steps.
sserve, serves static content using a locally-trusted certificate, generated with the well-knowedmkcert tool.
When you install sserve it automatically creates and installs a local CA in the system (and browsers) root store, and generates the certificate for you.
No configuration is required, just lunch the tool and we take care of everything you need.
The supported root stores are the one supported by mkcert.
Checkout the updated listhere.
Here there is a handy copy:
- macOS system store
- Windows system store
- Linux variants that provide either
update-ca-trust
(Fedora, RHEL, CentOS) orupdate-ca-certificates
(Ubuntu, Debian) ortrust
(Arch)
- Firefox (macOS and Linux only)
- Chrome and Chromium
- Java (when
JAVA_HOME
is set)
https-localhost is compatible with the LTS and latest version of Node.js.
If you need compatibility with other Node.js versions let me know, we'll try to rearrange the code.
- At first run this tool generate a trusted certificate. The sudo password may be required. If you cannot provide the sudo password generate a
localhost.key
andlocalhost.crt
and specify its path withCERT_PATH=/diractory/containing/certificates/ serve ~/myproj
. - At each run the password may be required to run the server on port 443 and 80. To avoid the script ask for password specify a different port number:
PORT=4433 serve ~/myproj
.
Run with sudo to use the default ports 443 and 80. You can also change port with:PORT=4433 serve ~/myproj
.
Another service on your machine is using port 443 or port 80. Stop it or change port withPORT=4433 serve ~/myproj
.
Windows users with spaces or quotes in the name (like Aldo D'Aquino) may experience some problems in running the script. You can try to escape this chars or put the entire path between double quotes, but I suggest you to switch to a better user name (like aldodaquino).
RangeError: Invalid typed array length: -4095
It is a known bug ofspdy
that is present sometimes with some old Node.js versions.
It should be present only withNODE_ENV=production
, hence the easiest fix is to avoid using the production env. Anyway, if you need the production env, you can try to update Node.js to the latest release, or to the most stable LTS version.
I've tried to reproduce this error without any success (checkout theTravis build logs). If you can help please open an issue and describe as better as you can how to reproduce it, I'll be happy to help you.
And in general all the cases when the script runs but the connection is marked as untrusted.
Force a reinstall of the certificate withREINSTALL=true serve
.sudo
may be required on linux and MacOS.
If the problem is solved you should be able to use https-localhost also as module.
Each contribute is welcome!
Please, checkout thecontributing guidelines.
Is released underAGPL-3.0 - GNU Affero General Public License v3.0.
- modification and redistribution allowed for both private andcommercial use
- you mustgrant patent right to the owner and to all the contributors
- you mustkeep it open source and distribute under thesame license
- changes must be documented
- include a limitation of liability and itdoes not provide any warranty
THIS TOOL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.For the full warranty check theLICENSE.
About
HTTPS server running on localhost
Topics
Resources
License
Code of conduct
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Languages
- JavaScript99.2%
- HTML0.8%