A
A
Alexey Yakovlev2020-05-23 09:39:21
Express.js
Alexey Yakovlev, 2020-05-23 09:39:21

I need to include static files on node js, what should I do?

server.js:

const path = require('path');
const express = require('express');

const app = express();
const server = require("http").createServer(app);
const io = require("socket.io").listen(server);

app.use('/dist', express.static('dist'));

app.get("/", (req, res) => { 
    res.sendFile(__dirname + "/index.html");
});

server.listen(3000, () => {
    console.log('Server started!');
});


I also use webpack and when compiling I get a new html file in the dist folder
Connection happens like this:
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Document</title>
<link href="main.fa66f4cb4759dd0c1a34.css" rel="stylesheet"></head>
<body>
    
    <h1>Hello World!</h1>

<script src="main.js"></script></body>
</html>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question