Skip to content

Commit 507bc51

Browse files
Merge pull request #8 from Yellow-Dog-Man/feat/esm
chore(refactor): swap to esm, its 2024 bro
2 parents df48b21 + 1ff92dd commit 507bc51

File tree

5 files changed

+33
-21
lines changed

5 files changed

+33
-21
lines changed

app.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
var createError = require('http-errors');
2-
var express = require('express');
3-
var path = require('path');
4-
var logger = require('morgan');
1+
import express from 'express';
2+
import path from 'path';
3+
import logger from "morgan";
54

6-
var indexRouter = require('./routes/index');
5+
import indexRouter from './routes/index.js';
6+
7+
// https://flaviocopes.com/fix-dirname-not-defined-es-module-scope/
8+
import { fileURLToPath } from 'url';
9+
const __filename = fileURLToPath(import.meta.url);
10+
const __dirname = path.dirname(__filename);
711

812
var app = express();
913

@@ -45,4 +49,4 @@ app.use(function(err, req, res, next) {
4549
res.render('error');
4650
});
4751

48-
module.exports = app;
52+
export default app;

bin/www bin/www.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
* Module dependencies.
55
*/
66

7-
var app = require('../app');
8-
var debug = require('debug')('go.resonite.com:server');
9-
var http = require('http');
7+
import app from '../app.js';
8+
9+
import debugModule from 'debug';
10+
const debug = new debugModule('go.resonite.com:server');
11+
12+
import http from 'http';
1013

1114
/**
1215
* Get port from environment and store in Express.

helpers/preprocessing.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import DOMPurify from 'isomorphic-dompurify';
2+
13
function preProcessName(name) {
24
const start = /<color="?(.+?)"?>/gi;
35
const end = /<\/color>/gi
@@ -27,7 +29,7 @@ function preProcessWorld(json) {
2729
return json;
2830
}
2931

30-
function preProcess(json, type) {
32+
export function preProcess(json, type) {
3133

3234
// ensure page title
3335
json.title = DOMPurify.sanitize(json.name);
@@ -43,6 +45,4 @@ function preProcess(json, type) {
4345
}
4446

4547
return json;
46-
}
47-
48-
module.exports.preProcess = preProcess;
48+
}

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
{
22
"name": "go.resonite.com",
33
"version": "0.0.0",
4+
"type": "module",
45
"private": true,
56
"scripts": {
6-
"start": "node ./bin/www"
7+
"start": "node ./bin/www.js"
78
},
9+
"exports": "./start.js",
810
"dependencies": {
911
"cookie-parser": "~1.4.4",
1012
"debug": "~2.6.9",

routes/index.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
var express = require('express');
2-
var createError = require('http-errors');
1+
import express from 'express';
2+
3+
import pkg from 'http-errors';
4+
const {createError} = pkg;
5+
6+
import { preProcess } from '../helpers/preprocessing.js';
7+
38
var router = express.Router();
4-
var DOMPurify = require("isomorphic-dompurify");
5-
var preprocessing = require('../helpers/preprocessing');
69

710
/* GET home page. */
811
router.get('/', function(req, res, next) {
@@ -54,7 +57,7 @@ async function handle(type, req, res, next) {
5457
return next(createError(400, "go.resonite.com only works for Session and world link."));
5558
}
5659

57-
json = preprocessing.preProcess(json, type);
60+
json = preProcess(json, type);
5861
json.urlPath = req.getUrl();
5962

6063
res.status(200).render(type, json);
@@ -74,7 +77,7 @@ async function handleJson(type, req, res, next) {
7477
return next();
7578
}
7679

77-
json = preprocessing.preProcess(json, type);
80+
json = preProcess(json, type);
7881
// title is the TOP link
7982
var title = getOpenGraphTitle(type);
8083
res.json({
@@ -98,4 +101,4 @@ function getOpenGraphTitle(type) {
98101
}
99102
}
100103

101-
module.exports = router;
104+
export default router;

0 commit comments

Comments
 (0)