New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@jsenv/server

Package Overview
Dependencies
Maintainers
2
Versions
219
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@jsenv/server

Write your Node.js server using pure functions

  • 12.7.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
489
increased by140.89%
Maintainers
2
Weekly downloads
 
Created
Source

server npm package

@jsenv/server helps to write flexible server code with a declarative API.

import { startServer } from "@jsenv/server"

await startServer({
  protocol: "http",
  port: 8080,
  requestToResponse: () => {
    return {
      status: 200,
      headers: {
        "content-type": "text/plain",
      },
      body: "Hello world",
    }
  },
})

Example

Code starting a server and using node-fetch to send a request on that server:

import fetch from "node-fetch"
import { startServer } from "@jsenv/server"

const server = await startServer({
  requestToResponse: () => {
    return {
      status: 200,
      headers: {
        "content-type": "text/plain",
      },
      body: "Hello world",
    }
  },
})

const response = await fetch(server.origin)
const responseBodyAsText = await response.text()
console.log(responseBodyAsText) // "Hello world"

Code starting a server with 2 request handlers:

/*
 * starts a server which:
 * - when requested at "/"
 *   -> respond with 200
 * - otherwise
 *   -> respond with 404
 */
import { startServer, composeServices } from "@jsenv/server"

const indexService = (request) => {
  if (request.ressource === "/") {
    return { status: 200 }
  }
  return null // means "I don't handle that request"
}

const notFoundService = (request) => {
  return { status: 404 }
}

const server = await startServer({
  requestToResponse: composeServices({
    index: indexService,
    otherwise: notFoundService,
  }),
})

const fetch = await import("node-fetch")
const responseForOrigin = await fetch(server.origin)
responseForOrigin.status // 200

const responseForFoo = await fetch(`${server.origin}/foo`)
responseForFoo.status // 404

Code starting a server in https:

import { readFileSync } from "node:fs"
import { startServer } from "@jsenv/server"

await startServer({
  protocol: "https",
  certificate: readFileSyncAsString("./server.crt"),
  privateKey: readFileSyncAsString("./server.key"),
  allowHttpRequestOnHttps: true,
  requestToResponse: (request) => {
    const clientUsesHttp = request.origin.startsWith("http:")

    return {
      status: 200,
      headers: {
        "content-type": "text/plain",
      },
      body: clientUsesHttp ? `Welcome http user` : `Welcome https user`,
    }
  },
})

function readFileSyncAsString(relativeUrl) {
  const fileUrl = new URL(relativeUrl, import.meta.url)
  return String(readFileSync(fileUrl))
}

Code starting a server for static files:

import { startServer, fetchFileSystem } from "@jsenv/server"

await startServer({
  requestToResponse: async (request) => {
    const fileUrl = new URL(request.ressource.slice(1), import.meta.url)
    const response = await fetchFileSystem(fileUrl, {
      ...request,
    })
    return response
  },
})

Documentation

Installation

npm install @jsenv/server

FAQs

Package last updated on 21 Jun 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc