Skip to content

Commit

Permalink
feat: migrate to es module
Browse files Browse the repository at this point in the history
  • Loading branch information
sinedied committed Dec 6, 2022
1 parent 3c311ce commit 9a4aaa0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
5 changes: 3 additions & 2 deletions bin/nanog
Original file line number Diff line number Diff line change
@@ -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'
});
Expand Down
26 changes: 14 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -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'));
Expand Down Expand Up @@ -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'));
Expand All @@ -55,4 +57,4 @@ module.exports = function (configPath) {
.createServer({key, cert, ca}, app)
.listen(config.https.port || 8443, showAddress.bind(this, 'https'));
}
};
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down

0 comments on commit 9a4aaa0

Please sign in to comment.