Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

dx-server

Package Overview
Dependencies
Maintainers
1
Versions
55
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dx-server

  • 0.0.1-pre-4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
7
decreased by-30%
Maintainers
1
Weekly downloads
 
Created
Source

dx-server - modern, unopinionated, and satisfactory server

Install

yarn add dx-server jchain

Usage

Check below sample with comment for more details.

import {Server} from 'http'
import {promisify} from 'util'
import {
	requestContext, responseContext,

	expressContext, setHtml, setJson,

	bufferBodyContext,
	jsonBodyContext,
	queryContext,
	rawBodyContext,
	textBodyContext,
	urlencodedBodyContext,

	router,
	catchApiError, catchError, notFound, notFoundApi,
	expressApp,
} from 'dx-server'

const tcpServer = new Server()
	.on('request', async (req, res) => {
	try {
		await chain(
			requestContext.chain(req), // required for most middlewares
			responseContext.chain(res), // required for most middlewares
			expressContext.chain({jsonBeautify: true}), // allows to use setHtml, setJson, setRaw, setBuffer, setFile, setRedirect, etc.
			bufferBodyContext.chain(), // use raw buffer body as Buffer use bufferBodyContext.value. This is required for jsonBodyContext, urlencodedBodyContext, textBodyContext, rawBodyContext
			jsonBodyContext.chain(), // to get body parsed as json use jsonBodyContext.value. Only available if content-type is application/json
			urlencodedBodyContext.chain(), // to get body parsed as urlencoded use urlencodedBodyContext.value. Only available if content-type is application/x-www-form-urlencoded
			textBodyContext.chain(), // to get body parsed as text use textBodyContext.value. Only available if content-type is text/plain
			rawBodyContext.chain(), // to get body as raw use rawBodyContext.value. Only available if content-type is application/octet-stream
			queryContext.chain(), // to get query params use queryContext.value. Query is parsed via 'qs' package. If no query, return empty object {}
			next => {
				// this is the difference between express and dx-server
				// req, res can be accessed from anywhere via context which uses NodeJS's AsyncLocalStorage under the hood
				responseContext.value.setHeader('cache-control', 'no-cache')
				next()
			},
			async next => {
				// global error catching for all following middlewares
				try {
					await next()
				} catch (e) {
					console.error(e)
					setHtml('internal server error', {status: 500})
				}
			},
			expressApp(app => {
				// any express feature can be used
				// required express installed, with for e.g., `yarn add express`
				app.use('/photos', express.static('photos'))
			}),
			// example of catching error for all /api/* routes
			router.post({
				async '/api'({next}) {
					try {
						await next()
					} catch (e) {
						console.error(e)
						setJson({
							message: 'internal server error',
							code: 'internal_server_error'
						}, {status: 500})
					}
				}
			}, {end: false}), // note: {end: false} is required to match all /api/* routes. This option is passed directly to path-to-regexp
			router.post({
				'/api/me'() { // sample POST router
				setJson({name: 'joe'})
			}
			}),
			router.get({ // sample GET router
				'/'() {
					setHtml('ok')
				},
				'/health'() {
					setHtml('ok')
				}
			}),
			router.post({ // api not found router
				'/api'() {
					setJson({
						message: 'not found',
						code: 'not_found'
					}, {status: 404})
				}
			}, {end: false}),
			() => { // not found router
				setHtml('not found', {status: 404})
			},
		)()
	} catch (e) {
	console.error(e)
	}
})

const port = +(process.env.PORT ?? 3000)
await promisify(tcpServer.listen.bind(tcpServer))(port)
console.log(`server is listening at ${port}`)

TODO

Until these middlewares are available as native dx-server middlewares, express middlewares can be used with expressApp()

  • native static file serve, like 'static-serve'
  • logger like morgan
  • cookie, session
  • cors

FAQs

Package last updated on 10 Aug 2023

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