From 9a4aaa0b101e1328bceb504384aabd0e027f96af Mon Sep 17 00:00:00 2001 From: sinedied Date: Tue, 6 Dec 2022 16:28:55 +0100 Subject: [PATCH] feat: migrate to es module --- bin/nanog | 5 +++-- index.js | 26 ++++++++++++++------------ package.json | 3 ++- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/bin/nanog b/bin/nanog index fb110be..33d5aa0 100755 --- a/bin/nanog +++ b/bin/nanog @@ -1,9 +1,10 @@ #!/usr/bin/env node -const gateway = require('../index'); +import gateway from '../index'; +import minimist from 'minimist'; const base62 = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; const help = 'Usage: nanog [--config file.yml] [--keygen]\n'; -const args = require('minimist')(process.argv.slice(2), { +const args = minimist(process.argv.slice(2), { boolean: ['help', 'keygen'], string: 'config' }); diff --git a/index.js b/index.js index 2a52ef4..a523e77 100644 --- a/index.js +++ b/index.js @@ -1,13 +1,14 @@ -const https = require('node:https'); -const fs = require('node:fs'); -const path = require('node:path'); -const express = require('express'); -const proxy = require('express-http-proxy'); -const rewrite = require('express-urlrewrite'); -const foreach = require('lodash.foreach'); -const yaml = require('js-yaml'); - -module.exports = function (configPath) { +import https from 'node:https'; +import fs from 'node:fs'; +import path from 'node:path'; +import process from 'node:process'; +import express from 'express'; +import proxy from 'express-http-proxy'; +import rewrite from 'express-urlrewrite'; +import foreach from 'lodash.foreach'; +import yaml from 'js-yaml'; + +export default function gateway(configPath) { configPath = configPath || process.env.NANO_GATEWAY_CONFIG || 'config.yml'; const config = yaml.load(fs.readFileSync(configPath, 'utf8')); @@ -41,7 +42,8 @@ module.exports = function (configPath) { console.log(`| ${type}://${bound.address}:${bound.port}`); } - console.log(`nano-gateway v${require('./package.json').version} started at:`); + const pkg = JSON.parse(fs.readFileSync('./package.json', 'utf8')); + console.log(`nano-gateway v${pkg.version} started at:`); if (config.http || !config.https) { this.http = app.listen(config.http.port || 8080, config.http.host, showAddress.bind(this, 'http')); @@ -55,4 +57,4 @@ module.exports = function (configPath) { .createServer({key, cert, ca}, app) .listen(config.https.port || 8443, showAddress.bind(this, 'https')); } -}; +} diff --git a/package.json b/package.json index a7ba4ba..70b7be7 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "Lightweight API gateway for home-scale projects", "repository": "sinedied/nano-gateway", "preferGlobal": true, - "main": "index.js", + "type": "module", + "exports": "./index.js", "bin": { "nanog": "./bin/nanog" },