npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

engine-express

v1.0.3

Published

Express wrapper to manage multiple APIs from a single server

Downloads

12

Readme

A lightweight Express wrapper that enables multiple APIs to be served from a single server given just the directory path and domain basename.

MIT license npm version npm size

Installation

Engine-Express is available as an npm package.

// with npm
npm install engine-express

Quick Start

./index.js

const engine = require("engine-express")
const path = require("path")

var api_list = [
	{
		"routes_path":  path.join(__dirname, RELATIVE_PATH_TO_YOUR_API1_ROUTES_FILE),
		"basename":  "/api1"
	},
	{
		"routes_path":  path.join(__dirname, RELATIVE_PATH_TO_YOUR_API2_ROUTES_FILE),
		"basename":  "/api2"
	},
]

engine.start(PORT_NUMBER, api_list);

API1_ROUTES_FILE.js and API2_ROUTES_FILE.js

const router = require("express").Router()

router.route("/route").get((req, res) => res.send("Hello World! This is a route"))
router.get('/*', (req, res) => res.send("Hello World! This is API 2"))

module.exports = router

Documentation: Check out the documentation here.

Example Projects: All examples in this document can be found fully implemented here.

Overview: Engine-Express enables a node server to serve multiple Express APIs from the same port. If you want to configure the underlying server before listening, simply access engine.server to make modifications to the underlying express application. To use the tool and start the server, simply pass the configuration parameters to the start function. Besides the port number and the optional HTTPS credentials, the start function will also take an array of APIs in the format of a basename and path. The basename represents the subdomain of the server where the API should be accessible, and the path represents the location of the routes that make up the API.

The specified routes file should export a Router, as well as define as act as the head of the routes. It is possible to both exhaustively define all routes in this file, as well as simply defining routes that filter to subsequent routes in other files. Both of these cases are demonstrated below in the Routes File Examples section.

API

Name | Type | Description -----|------|--------- start| function| start starts the server and serves the APIs. Learn more about the start function here. server| property| server property provides direct access to the underlying server. Learn more about the server property here.

Index File Examples

Example with HTTPS

const engine = require("engine-express")

var api_list = [
	{
		"routes_path":  PATH_TO_ROUTES_FILE,
		"basename":  "/api1"
	},
	{
		"routes_path":  PATH_TO_ROUTES_FILE,
		"basename":  "/api2"
	},
]

var https_creds = {
    "certificate_path": PATH_TO_HTTPS_CERTIFICATE,
    "private_key_path": PATH_TO_HTTPS_PRIVATE_KEY
}

engine.start(5000, api_list, https_creds);

Example with server configurations

const engine = require("engine-express")
const fs = require("fs")
const path = require("path")
const bodyParser = require("body-parser")
const cors = require("cors")
const morgan = require("morgan")
const fileUpload= require("express-fileupload")

var api_list = [
	{
		"routes_path":  PATH_TO_ROUTES_FILE,
		"basename":  "/api1"
	},
	{
		"routes_path":  PATH_TO_ROUTES_FILE,
		"basename":  "/api2"
	},
]
engine.server.use(bodyParser.json())
engine.server.use(cors())
engine.server.use(fileUpload())
engine.server.use(morgan('combined', { stream: fs.createWriteStream(path.join(__dirname, '../engine-express.log'), { flags: 'a' }) }))

engine.start(5000, api_list);

Routes File Examples

Example with controllers

ROUTES_FILE.js

const router = require("express").Router()
const controllers = require("./controllers")

router.get('/*', controllers.sendResponse)
module.exports = router

./controllers.js

exports.sendResponse = (req, res) => res.send("Hello World")

Example with routing

ROUTES_FILE.js

const router = require("express").Router()

router.use('/public', require("./publicRoutes"))
router.use('/private', require("./privateRoutes"))

module.exports = router

./publicRoutes

const publicRouter = require("express").Router()

publicRouter.get("/*",(req, res) => res.send("Public page: Hello World"))

module.exports = publicRouter

./privateRoutes

const privateRouter = require("express").Router()

privateRouter.get("/*",(req, res) => res.send("Private page: Hello World"))

module.exports = privateRouter

Example with middleware

ROUTES_FILE.js

const router = require("express").Router()

const log = (req, res, next) => {
	console.log("Request recieved")
	next()
}

const authenticate = (req, res, next) => {
	if (Math.random() > .5){
		console.log(`${req.params.name ? req.params.name : "An unamed user"} had improper credentials`)
		res.send("Access denied");
	}
	else{
		console.log(`${req.params.name ? req.params.name : "An unamed user"} had proper credentials`)
		next()
	}
}

const respond = (req, res) => res.send(`${req.params.name ? req.params.name : "An unamed user"} says 'Hello World!'`)

router.get('/:name?', log, authenticate, respond)

module.exports = router