简体   繁体   English

不使用EJS链接CSS文件

[英]Not link css file with ejs

I am working with simple express node js But my main.css file does not link with header.ejs both are the same folder shown in below picture. 我正在使用简单的快速节点js,但是我的main.css文件未与header.ejs链接,两者都是下图所示的同一文件夹。

I have searched on google but the problem still there. 我在Google上进行了搜索,但问题仍然存在。 my app.js file code 我的app.js文件代码

    var express = require("express")
    var app = express()
    var request = require("request");
    app.set("view engine", "ejs");

    app.get("/", function(req, res){
        res.render("search");
    });
    app.get("/results", function(req, res){
        var url ="http://www.omdbapi.com/?s="+req.query.search+"&apikey=my_api_key"
        request(url, function(error, response, body){
            if(!error && response.statusCode == 200){
                var data = JSON.parse(body)
                // res.send(results["Search"]);
                res.render("results",{data: data});
            }
        })
    });


    app.listen(5000,"localhost",function(){
        console.log("movie search");
    })

在此处输入图片说明

You have a wrong setup, Your view folder should only contain view files (in your case *.ejs files) but not static content. 设置错误,您的视图文件夹应仅包含视图文件(在您的情况下为* .ejs文件),而不包含静态内容。 Create another folder with name 'public' at root of your node project and move your main.css there and add the following code to serve static content. 在节点项目的根目录中创建另一个名为“ public”的文件夹,并将main.css移至该文件夹,然后添加以下代码以提供静态内容。

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

After this you should be able to access your main.css using localhost:5000/main.css If you see your content, The css import in your view file will start working. 之后,您应该可以使用localhost:5000 / main.css访问main.css。如果看到内容,则视图文件中的css导入将开始工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM