Skip to content

Commit 9d261f9

Browse files
authored
Merge pull request #11 from NucleoidAI/add-port-option
Add port option
2 parents 11e5785 + 6487577 commit 9d261f9

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

bin.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,19 @@ require("yargs")
55
command: "start [folderName]",
66
desc: "Start HTTP server",
77
builder: (yargs) => {
8-
yargs.positional("folderName", {
9-
describe: "Set folder name for the server",
10-
default: "dist",
11-
});
8+
yargs
9+
.positional("folderName", {
10+
describe: "Set folder name for the server",
11+
default: "dist",
12+
})
13+
.option("port", {
14+
alias: "p",
15+
describe: "Port to run the server on",
16+
type: "number",
17+
default: 3000,
18+
});
1219
},
13-
handler: (argv) => require("./server").serve(argv.folderName),
20+
handler: (argv) => require("./server").serve(argv.folderName, argv.port),
1421
})
1522
.demandCommand().argv;
23+

server.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,31 @@ const morgan = require("morgan");
33
const path = require("path");
44
const { pathToFileURL, fileURLToPath } = require("url");
55

6-
function serve(folderName) {
6+
function serve(folderName, port) {
77
const configPath = pathToFileURL(path.resolve(process.cwd()));
88

99
import(`${configPath}/config.mjs`).then(({ default: config }) => {
1010
const app = express();
1111
app.use(morgan("tiny"));
1212

1313
const staticPath = path.join(fileURLToPath(configPath), folderName);
14+
1415
app.use(
1516
config.base || "/",
16-
express.static(staticPath, { redirect: false }), // Disable automatic redirection in static middleware
17+
express.static(staticPath, { redirect: false }) // Disable automatic redirection in static middleware
1718
);
1819

1920
app.get("*", (req, res) => {
2021
res.sendFile(path.join(staticPath, "index.html"));
2122
});
2223

23-
const port = process.env.PORT || config.port || 3000;
24+
const serverPort = process.env.PORT || port || config.port || 3000;
2425

25-
app.listen(port, () => {
26+
app.listen(serverPort, () => {
2627
console.log(`Server running on port ${port} with base ${config.base}`);
2728
});
2829
});
2930
}
3031

3132
module.exports = { serve };
33+

0 commit comments

Comments
 (0)