-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite_express_server.js
84 lines (70 loc) · 2.24 KB
/
vite_express_server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
Project Name: solid-js-sandbox
License: MIT
Created by: Lightnet
*/
import fs from 'fs'
import path from 'path'
import express from 'express'
import { fileURLToPath } from 'url';
import { dirname } from 'path';
//import Gun from "gun";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
import { createServer as createViteServer } from 'vite'
import routeAPI from "./src/server/api.js"
import { setupDatabase } from './libs/db/mongoose/database.js';
//import * as vite from 'vite'
console.log("script server.js")
async function createServer() {
await setupDatabase();
const app = express()
// Create Vite server in middleware mode
const vite = await createViteServer({
server: { middlewareMode: true },
//appType: './index.html' // don't include Vite's default HTML handling middlewares
appType: 'custom' // don't include Vite's default HTML handling middlewares
})
// Use vite's connect instance as middleware
app.use(vite.middlewares)
app.use(express.json());
app.use('/api',routeAPI);
app.use('*',async (req, res) => {
const url = req.originalUrl
let template = fs.readFileSync(
path.resolve(__dirname, 'index.html'),
'utf-8'
)
template = await vite.transformIndexHtml(url, template)
const html = template;
res.status(200).set({ 'Content-Type': 'text/html' }).end(html);
});
//app.use('*', async (req, res) => {
// Since `appType` is `'custom'`, should serve response here.
// Note: if `appType` is `'spa'` or `'mpa'`, Vite includes middlewares to handle
// HTML requests and 404s so user middlewares should be added
// before Vite's middlewares to take effect instead
//})
const PORT = 3000;
const server = app.listen(PORT, err => {
if (err) throw err;
//console.log(app);
console.log(`> Running on http://localhost:`+PORT);
})
//var gun;
//gun = Gun({
//web: server
//});
//gun.on("hi", peer => {
//peer connect
//console.log('connect peer to',peer);
//console.log("peer connect!");
//});
//gun.on("bye", peer => {
// peer disconnect
//console.log('disconnected from', peer);
//console.log("disconnected from peer!");
//});
console.log("init server...")
}
createServer()