mirror of
https://github.com/Alamantus/Lexiconga.git
synced 2025-06-04 16:26:36 +02:00
Install & try HyperExpress
This commit is contained in:
parent
4ea91cd681
commit
5f2b4b63d9
8 changed files with 572 additions and 303 deletions
748
package-lock.json
generated
748
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -25,10 +25,8 @@
|
|||
},
|
||||
"devDependencies": {
|
||||
"dotenv": "^16.4.5",
|
||||
"esbuild": "^0.21.3",
|
||||
"minify-html-literals": "^1.3.5",
|
||||
"nodemon": "^3.1.3",
|
||||
"sass": "^1.77.4",
|
||||
"sharp": "^0.29.3"
|
||||
},
|
||||
"dependencies": {
|
||||
|
@ -38,10 +36,13 @@
|
|||
"@fastify/static": "^7.0.4",
|
||||
"@fastify/websocket": "^10.0.1",
|
||||
"bulma": "^1.0.1",
|
||||
"esbuild": "^0.21.5",
|
||||
"fastify": "^4.27.0",
|
||||
"hyper-express": "^6.16.3",
|
||||
"marked": "^3.0.8",
|
||||
"papaparse": "^5.4.1",
|
||||
"require-overrides": "^0.3.0",
|
||||
"sass": "^1.77.4",
|
||||
"uhtml": "^4.5.8",
|
||||
"uhtml-ssr": "^0.9.1",
|
||||
"upup": "^1.1.0"
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { render } from 'uhtml';
|
||||
import header from './elements/body/header';
|
||||
import main from './elements/body/main';
|
||||
import footer from './elements/body/footer';
|
||||
const { render } = require('uhtml');
|
||||
const header = require('./elements/body/header');
|
||||
const main = require('./elements/body/main');
|
||||
const footer = require('./elements/body/footer');
|
||||
|
||||
(() => {
|
||||
class App {
|
||||
|
|
36
src/routes/assets.js
Normal file
36
src/routes/assets.js
Normal file
|
@ -0,0 +1,36 @@
|
|||
const path = require('node:path');
|
||||
const esbuild = require('esbuild');
|
||||
const sass = require('sass');
|
||||
const HyperExpress = require('hyper-express');
|
||||
const router = new HyperExpress.Router();
|
||||
|
||||
router.get('/lexiconga.js', async (request, response) => {
|
||||
const filePath = path.join(process.cwd(), '/src/lexiconga.js');
|
||||
return response.type('text/javascript').sendFile(() => {
|
||||
const build = esbuild.buildSync({
|
||||
entryPoints: [filePath],
|
||||
sourcemap: false,
|
||||
write: false,
|
||||
bundle: true,
|
||||
minify: true,
|
||||
treeShaking: true,
|
||||
platform: 'browser',
|
||||
format: 'iife',
|
||||
target: 'es2015',
|
||||
});
|
||||
console.log(build);
|
||||
const file = build.outputFiles[0];
|
||||
return file.contents;
|
||||
});
|
||||
});
|
||||
|
||||
router.get('styles.css', async (request, response) => {
|
||||
const filePath = path.join(process.cwd(), '/src/styles.scss');
|
||||
return response.type('text/css').sendFile(() => {
|
||||
const compiledSass = sass.compile(filePath, { style: 'compressed' });
|
||||
return compiledSass.css;
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
22
src/routes/web.js
Normal file
22
src/routes/web.js
Normal file
|
@ -0,0 +1,22 @@
|
|||
const path = require('path');
|
||||
const sass = require('sass');
|
||||
const overrides = require('require-overrides');
|
||||
overrides.set('uhtml', 'uhtml-ssr'); // Replace instances of `uhtml` with `uhtml-ssr` so Node can return strings from the generated html
|
||||
const { render } = require('uhtml-ssr');
|
||||
const HyperExpress = require('hyper-express');
|
||||
const router = new HyperExpress.Router();
|
||||
|
||||
router.get('/', async (request, response) => {
|
||||
return response.type('text/html').sendFile(() => {
|
||||
return require('../pages/index')();
|
||||
});
|
||||
});
|
||||
|
||||
router.get('/help', async (request, response) => {
|
||||
return response.type('text/html').sendFile(() => {
|
||||
return require('../pages/help')();
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
17
src/server-fastify.js
Normal file
17
src/server-fastify.js
Normal file
|
@ -0,0 +1,17 @@
|
|||
require('dotenv').config();
|
||||
|
||||
const fastify = require('fastify')()
|
||||
.register(require('@fastify/helmet'))
|
||||
// .register(require('@fastify/mysql'), {
|
||||
// connectionString: 'mysql://root@localhost/mysql',
|
||||
// })
|
||||
.register(require('@fastify/jwt'), {
|
||||
secret: process.env.JWT_SECRET,
|
||||
})
|
||||
.register(require('@fastify/websocket'))
|
||||
.register(require('./routes/routes'));
|
||||
|
||||
fastify.listen({ port: 3000 }, err => {
|
||||
if (err) throw err;
|
||||
console.log(`server listening on http://localhost:${fastify.server.address().port}`);
|
||||
});
|
|
@ -1,17 +1,28 @@
|
|||
const HyperExpress = require('hyper-express');
|
||||
require('dotenv').config();
|
||||
|
||||
const fastify = require('fastify')()
|
||||
.register(require('@fastify/helmet'))
|
||||
// .register(require('@fastify/mysql'), {
|
||||
// connectionString: 'mysql://root@localhost/mysql',
|
||||
// })
|
||||
.register(require('@fastify/jwt'), {
|
||||
secret: process.env.JWT_SECRET,
|
||||
})
|
||||
.register(require('@fastify/websocket'))
|
||||
.register(require('./routes'));
|
||||
const server = new HyperExpress.Server();
|
||||
server.files = {};
|
||||
|
||||
server.use((req, res, next) => {
|
||||
req.hasFile = typeof server.files[req.path] !== 'undefined';
|
||||
res.sendFile = (setFile = () => {}) => {
|
||||
if (!req.hasFile) {
|
||||
server.files[req.path] = setFile();
|
||||
}
|
||||
return res.send(server.files[req.path]);
|
||||
};
|
||||
|
||||
next();
|
||||
});
|
||||
|
||||
server.use('/', require('./routes/assets'));
|
||||
server.use('/', require('./routes/web'));
|
||||
|
||||
const port = process.env.APP_PORT ?? 8080;
|
||||
server.listen(port)
|
||||
.then(socket => console.log(`Web server started on http://localhost:${port}`))
|
||||
.catch(error => console.error('Failed to start web server: ', error));
|
||||
|
||||
return server;
|
||||
|
||||
fastify.listen({ port: 3000 }, err => {
|
||||
if (err) throw err;
|
||||
console.log(`server listening on http://localhost:${fastify.server.address().port}`);
|
||||
});
|
Loading…
Add table
Reference in a new issue