12 |
13 |
14 |
--------------------------------------------------------------------------------
/app.js:
--------------------------------------------------------------------------------
1 | let https = require('https');
2 | let fs = require('fs');
3 | let path = require('path');
4 |
5 | https.createServer({
6 | key: fs.readFileSync('./localhost-key.pem'),
7 | cert: fs.readFileSync('./localhost.pem'),
8 | },
9 | function(request, response) {
10 | if(request.url === '/') request.url = '/index.html'
11 | fs.readFile(`./public${request.url}`, function (err, data) {
12 | if (err) {
13 | response.writeHead(500, { 'Content-Type': 'text/plain' });
14 | response.end('Erreur interne.');
15 | } else {
16 | switch(request.url.split('.')[request.url.split('.').length - 1]) {
17 | case 'html': response.writeHead(200, { 'Content-Type': 'text/html' }); break;
18 | case 'css': response.writeHead(200, { 'Content-Type': 'text/css' }); break;
19 | case 'js': response.writeHead(200, { 'Content-Type': 'text/javascript' }); break;
20 | }
21 | response.end(data);
22 | }
23 | });
24 | })
25 | .listen(3000);
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2025 Charles Chrismann
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # node-local-https-dev-server
2 |
3 | > [!WARNING]
4 | > If you are a developer passing by, what you are probably looking for is already provided by [Vite](https://vite.dev/config/server-options.html#server-https).
5 |
6 | This is a simple local https server.
7 |
8 | It was initially created in order to be able to use certain js front-end functionalities which require the https protocol to operate, such as the browser sensor API. Connecting to this url will be considered non-private or dangerous by browsers.
9 |
10 | ## Setup
11 |
12 | ```
13 | git clone https://github.com/Charles-Chrismann/node-local-https-dev-server.git
14 | ```
15 |
16 | ```
17 | cd node-local-https-dev-server/
18 | ```
19 |
20 | ```
21 | npm i
22 | ```
23 |
24 | At this point the last thing you need to do is create certificate and its private key using [mkcert](https://github.com/FiloSottile/mkcert).
25 |
26 | Install mkcert:
27 | - On Windows with choco for example
28 |
29 | ```
30 | choco install mkcert
31 | ```
32 |
33 | - On macOS with Homebrew
34 |
35 | ```
36 | brew install mkcert
37 | brew install nss # if you use Firefox
38 | ```
39 |
40 | - On Linux with Homebrew
41 |
42 | ```
43 | // TODO => see https://github.com/FiloSottile/mkcert#linux
44 | ```
45 |
46 | Once installed
47 |
48 | ```
49 | mkcert -install
50 | ```
51 |
52 | Then nvaigate to the root of the folder of this repo and run:
53 |
54 | ```
55 | mkcert localhost
56 | ```
57 |
58 | Two Files in .pem should now be created.If they are now already named like follow, rename the certificate as "localhost.pem" and the key as "localhost-key.pem", you can also modify app.js if you don't want to rename them.
59 |
60 | Thats it ! Now you can put your html/css/js files in public/ folder. No need to update file path
61 |
62 | PS: note that this repo is for now only for small projects and complex folder structure may not work.
63 |
64 | To start the https server run
65 |
66 | ```
67 | node ./app.js
68 | ```
69 |
70 | You can access your project in `https://127.0.0.1:3000`
71 |
--------------------------------------------------------------------------------