How can I include css files using node, express, and ejs?

前端 未结 11 824
执念已碎
执念已碎 2020-12-01 01:37

I\'m trying to follow the instructions to https://stackoverflow.com/a/18633827/2063561, but I still can\'t get my styles.css to load.

From app.js

ap         


        
相关标签:
11条回答
  • 2020-12-01 02:17

    Use in your main .js file:

    app.use('/css',express.static(__dirname +'/css'));
    

    use in you main .html file:

    <link rel="stylesheet" type="text/css" href="css/style.css" />
    

    The reason you getting an error because you are using a comma instead of a concat + after __dirname.

    0 讨论(0)
  • 2020-12-01 02:20

    I have used the following steps to resolve this problem

    1. create new folder (static) and move all js and css file into this folder.
    2. then add app.use('/static', express.static('static'))
    3. add css like < link rel="stylesheet" type="text/css" href="/static/style.css"/>
    4. restart server to view impact after changes.
    0 讨论(0)
  • 2020-12-01 02:28

    In your app or server.js file include this line:

    app.use(express.static('public'));

    In your index.ejs, following line will help you:

    <link rel="stylesheet" type="text/css" href="/css/style.css" />

    I hope this helps, it did for me!

    0 讨论(0)
  • 2020-12-01 02:29

    For NodeJS I would get the file name from the res.url, write the header for the file by getting the extension of the file with path.extname, create a read stream for the file, and pipe the response.

    const http = require('http');
    const fs = require('fs');
    const path = require('path');
    const port = process.env.PORT || 3000;
    
    const server = http.createServer((req, res) => {
        let filePath = path.join(
            __dirname,
            "public",
            req.url === "/" ? "index.html" : req.url
        );
    
        let extName = path.extname(filePath);
        let contentType = 'text/html';
    
        switch (extName) {
            case '.css':
                contentType = 'text/css';
                break;
            case '.js':
                contentType = 'text/javascript';
                break;
            case '.json':
                contentType = 'application/json';
                break;
            case '.png':
                contentType = 'image/png';
                break;
            case '.jpg':
                contentType = 'image/jpg';
                break;
        }
    
        console.log(`File path: ${filePath}`);
        console.log(`Content-Type: ${contentType}`)
    
        res.writeHead(200, {'Content-Type': contentType});
    
        const readStream = fs.createReadStream(filePath);
        readStream.pipe(res);
    });
    
    server.listen(port, (err) => {
        if (err) {
            console.log(`Error: ${err}`)
        } else {
            console.log(`Server listening at port ${port}...`);
        }
    });
    
    0 讨论(0)
  • 2020-12-01 02:31

    The custom style sheets that we have are static pages in our local file system. In order for server to serve static files, we have to use,

    app.use(express.static("public"));
    

    where,

    public is a folder we have to create inside our root directory and it must have other folders like css, images.. etc

    The directory structure would look like :

    Then in your html file, refer to the style.css as

    <link type="text/css" href="css/styles.css" rel="stylesheet">
    
    0 讨论(0)
  • 2020-12-01 02:33

    Use this in your server.js file

    app.use(express.static('public'));

    without the directory ( __dirname ) and then within your project folder create a new file and name it public then put all your static files inside it

    0 讨论(0)
提交回复
热议问题