Code Monkey home page Code Monkey logo

express-start's People

Contributors

chriscalo avatar dependabot[bot] avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

express-start's Issues

Stop listening for `process.env.PORT` and add `autoPort` option

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:

  1. the port parameter in start(server, port) defaults to 8000 when a falsy
    value or no value is provided
  2. it first tries to listen on port, incrementing it until one is found that
    is available (8001, 8002, 8003, etc)

Bug: Cannot access 'listener' before initialization

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

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 and hostname. If the hostname 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 of 0, to have the operating system assign a random port, which can be retrieved by using server.address().port after the 'listening' event has been emitted.

Add body parsing.

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.