Skip to content

Commit

Permalink
Include port number from env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
canmingir committed Jan 18, 2024
1 parent fae3747 commit 40e45d8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 18 deletions.
3 changes: 1 addition & 2 deletions bin.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#!/usr/bin/env node
require("yargs")
.scriptName("httpserver")
.scriptName("http-server")
.command({
command: "start",
desc: ":start http server",
handler: () => require("./server.js"),
})
.demandCommand()
.argv.toString();

10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "http-server",
"version": "1.0.0",
"name": "@nucleoidjs/http-server",
"version": "0.0.0",
"description": "Create http-server with basename for SPA Projects.",
"bin": {
"http-server": "bin.js"
},
"scripts": {
"start": "node bin.js start",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "exit 0"
},
"repository": {
"type": "git",
Expand Down
15 changes: 7 additions & 8 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,20 @@ import(`${configPath}/config.js`).then((module) => {
const config = module.default;

app.use(
config.base,
express.static(path.join(fileURLToPath(configPath), "dist"))
config.base,
express.static(path.join(fileURLToPath(configPath), "dist")),
);

app.get("*", (req, res) => {
res.sendFile(path.join(fileURLToPath(configPath), "dist", "index.html"));
});

app.listen(config.port || 3000, () => {
const port = process.env.PORT || config.port || 3000;

app.listen(port, () => {
console.log(
`\x1b[36m%s\x1b[0m`,
`Server is running on port http://localhost:${config.port || 3000}${
config.base
}`
`\x1b[36m%s\x1b[0m`,
`Server running on port ${port} with base ${config.base}`,
);
});
});

0 comments on commit 40e45d8

Please sign in to comment.