chriscalo / express-start Goto Github PK
View Code? Open in Web Editor NEWStart an express server with automatic port selection.
License: MIT License
Start an express server with automatic port selection.
License: MIT License
It might be better to make use of environment variables more explicit, for example:
const express = require("express");
const { start } = require("express-start");
const server = express();
server.get("/", (req, res) => {
res.send("Hello, world!");
});
start(server, process.env.PORT || 8000);
The section on port selection logic from README.md then simplifies to:
The logic for selecting a port is as follows:
- the
port
parameter instart(server, port)
defaults to8000
when a falsy
value or no value is provided- it first tries to listen on
port
, incrementing it until one is found that
is available (8001
,8002
,8003
, etc)
The fix looks like this:
const { findPort } = require("./find-port");
async function start(server, startPort = 8000) {
return new Promise(async (resolve, reject) => {
const isDev = process.env.NODE_ENV !== "production";
const PORT = process.env.PORT || await findPort(startPort);
await server.listen(PORT, async function () {
console.log("");
if (isDev) {
const clipboardy = require("clipboardy");
const url = `http://localhost:${PORT}/`;
await clipboardy.write(url);
console.log(`App listening at ${url}`);
console.log(`URL copied to clipboard.`);
} else {
console.log(`App listening on port ${PORT}`);
}
console.log("Press Ctrl+C to quit.");
console.log("");
resolve(this);
});
});
};
// default export
module.exports = start;
// named export
module.exports.start = start;
Add option for hostname
when calling start(server)
.
From the Node docs:
server.listen([port][, hostname][, backlog][, callback])
Begin accepting connections on the specified
port
andhostname
. If thehostname
is omitted, the server will accept connections on any IPv6 address (::
) when IPv6 is available, or any IPv4 address (0.0.0.0
) otherwise. Omit the port argument, or use a port value of0
, to have the operating system assign a random port, which can be retrieved by usingserver.address().port
after the'listening'
event has been emitted.
It would be nice to be able to just paste the URL into a browser window once the server has started.
StackOverflow question:
https://stackoverflow.com/questions/7778539/copy-to-clipboard-in-node-js
Cross-platform clipboard module:
https://github.com/sindresorhus/clipboardy
What do you think of the following?
When
const { start } = require("express-start");
const server = express();
start(server, {
parseHttpBody: true,
});
then express-start
adds the express.json()
middleware.
Other sensible defaults like this could be added in the future.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.