Skip to content

Commit

Permalink
update: load index.html file 1/4 #3
Browse files Browse the repository at this point in the history
  • Loading branch information
mfunyu committed May 17, 2024
1 parent 41ace70 commit f07f7d1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
16 changes: 16 additions & 0 deletions server/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import path from 'path';
import process from 'process';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const dir_path = path.join(__dirname, process.env.DIRNAME || '../front/build');
const file_path = path.join(dir_path, process.env.FILENAME || 'index.html');

const config = {
PORT: process.env.PORT || 8000,
DIR_PATH: dir_path,
FILE_PATH: file_path
};

export default config;
12 changes: 5 additions & 7 deletions server/server.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import express from 'express';
import { createServer } from 'node:http';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
import { Server } from 'socket.io';
import config from './config.js';

const app = express();
const server = createServer(app);
const io = new Server(server);
const port = 8000;

const __dirname = dirname(fileURLToPath(import.meta.url));
app.use(express.static(config.DIR_PATH));

app.get('/', (req, res) => {
res.sendFile(join(__dirname, 'index.html'));
res.sendFile(config.FILE_PATH);
});

io.on('connection', (socket) => {
Expand All @@ -22,6 +20,6 @@ io.on('connection', (socket) => {
});
});

server.listen(port, () => {
console.log(`server running at http://localhost:${port}`);
server.listen(config.PORT, () => {
console.log(`server running at http://localhost:${config.PORT}`);
});

0 comments on commit f07f7d1

Please sign in to comment.