Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
https-localhost/index.js at master · batsheva-evers/https-localhost · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
batsheva-evers
https-localhost
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
https-localhost
/
index.js
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
executable file
·
121 lines (106 loc) · 3.93 KB
master
Breadcrumbs
https-localhost
/
index.js
Copy path
Top
File metadata and controls
Code
Blame
executable file
·
121 lines (106 loc) · 3.93 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env node
const
path
=
require
(
"path"
)
const
fs
=
require
(
"fs"
)
const
http
=
require
(
"http"
)
// spdy allows http2, while waiting express to support the http2 module
const
https
=
process
.
env
.
NODE_ENV
===
"production"
?
require
(
"spdy"
)
:
require
(
"https"
)
const
express
=
require
(
"express"
)
const
cors
=
require
(
"cors"
)
const
getCerts
=
require
(
path
.
resolve
(
__dirname
,
"certs.js"
)
)
.
getCerts
/* CONFIGURE THE SERVER */
// SSL certificate
const
createServer
=
(
domain
=
process
.
env
.
HOST
||
"localhost"
)
=>
{
// create a server with express
const
app
=
express
(
)
// add CORS headers to all responses
app
.
use
(
cors
(
)
)
// add getCerts to app
app
.
getCerts
=
getCerts
// override the default express listen method to use our server
app
.
listen
=
async
function
(
port
=
process
.
env
.
PORT
||
/* istanbul ignore next: cannot be tested on Travis */
443
)
{
app
.
server
=
https
.
createServer
(
await
getCerts
(
domain
)
,
app
)
.
listen
(
port
)
console
.
info
(
"Server running on port "
+
port
+
"."
)
return
app
.
server
}
// use gzip compression minify
if
(
process
.
env
.
NODE_ENV
===
"production"
)
{
const
compression
=
require
(
"compression"
)
const
minify
=
require
(
"express-minify"
)
app
.
use
(
compression
(
{
threshold
:
1
}
)
)
app
.
use
(
minify
(
)
)
app
.
set
(
"json spaces"
,
0
)
}
/* SETUP USEFUL FUNCTIONS */
// redirect http to https, usage `app.redirect()`
app
.
redirect
=
function
(
/* istanbul ignore next: cannot be tested on Travis */
httpPort
=
80
,
httpsPort
=
process
.
env
.
PORT
||
443
)
{
app
.
http
=
http
.
createServer
(
(
req
,
res
)
=>
{
const
reqHost
=
req
.
headers
.
host
?
req
.
headers
.
host
.
replace
(
":"
+
httpPort
,
""
)
:
/* istanbul ignore next: cannot be tested */
"localhost"
res
.
writeHead
(
301
,
{
Location
:
"https://"
+
reqHost
+
(
httpsPort
!==
443
?
":"
+
httpsPort
:
""
)
+
(
req
.
url
||
/* istanbul ignore next: cannot be tested */
""
)
}
)
res
.
end
(
)
}
)
.
listen
(
httpPort
)
console
.
info
(
"http to https redirection active."
)
}
// serve static content, usage `app.serve([path])`
app
.
serve
=
function
(
staticPath
=
process
.
cwd
(
)
,
port
=
process
.
env
.
PORT
||
/* istanbul ignore next: cannot be tested on Travis */
443
)
{
app
.
use
(
express
.
static
(
staticPath
)
)
// redirect 404 to 404.html or to index.html
app
.
use
(
(
req
,
res
)
=>
{
const
p404
=
staticPath
+
"/404.html"
const
index
=
staticPath
+
"/index.html"
// istanbul ignore else: not interesting
if
(
fs
.
existsSync
(
p404
)
)
res
.
status
(
404
)
.
sendFile
(
path
.
resolve
(
p404
)
)
else
if
(
fs
.
existsSync
(
index
)
)
res
.
status
(
200
)
.
sendFile
(
path
.
resolve
(
index
)
)
else
res
.
status
(
404
)
.
send
(
req
.
path
+
" not found."
)
}
)
console
.
info
(
"Serving static path: "
+
staticPath
)
app
.
listen
(
port
)
}
return
app
}
/* MAIN */
// usage: `serve [<path>]` or `node index.js [<path>]`
// istanbul ignore if: cannot be tested
if
(
require
.
main
===
module
)
{
const
app
=
createServer
(
)
// retrieve the static path from the process argv or use the cwd
// 1st is node, 2nd is serve or index.js, 3rd (if exists) is the path
app
.
serve
(
process
.
argv
.
length
===
3
?
process
.
argv
[
2
]
:
process
.
cwd
(
)
)
// redirect http to https (only if https port is the default one)
if
(
!
process
.
env
.
PORT
)
app
.
redirect
(
)
}
/* istanbul ignore next: cannot be tested */
process
.
on
(
"uncaughtException"
,
function
(
err
)
{
switch
(
err
.
errno
)
{
case
"EACCES"
:
console
.
error
(
"EACCES: run as administrator to use the default ports 443 and 80. "
+
"You can also change port with: `PORT=4433 serve ~/myproj`."
)
break
case
"EADDRINUSE"
:
console
.
error
(
"EADDRINUSE: another service on your machine is using "
+
"the current port.\nStop it or change port with:"
+
"`PORT=4433 serve ~/myproj`."
)
break
default
:
console
.
error
(
"Unexpected error "
+
err
.
errno
+
":\n\n"
+
err
)
break
}
process
.
exit
(
1
)
}
)
// export as module
module
.
exports
=
createServer
You can’t perform that action at this time.