【问题标题】:link front end files HTML, CSS , JS to the backend node.js将前端文件 HTML、CSS、JS 链接到后端 node.js
【发布时间】:2020-05-12 18:42:58
【问题描述】:

我是 Web 开发的新手,最近有一个关于 node.js 和 express 的在线课程。 本课程并未真正介绍如何在使用 Node 时添加 HTML、CSS 和 JS 文件。

我试过了:

var express = require('express');
var app = express ();
var port= 3000;

app.use(express.static('public'));
app.listen(port,function(){
console.log('Express app listening on port' +port);
})

app.get('/hello',function(request,response){
response.sendfile('index.html')
})

在这个 server.Js 文件中,我指出了应该在该页面上加载的 HTML 文件。 但是,它加载页面时没有样式,也没有加载其中的图像...

为了使我的前端文件在调用时可用,应该做些什么?

【问题讨论】:

    标签: javascript html css node.js express


    【解决方案1】:

    我认为您错过了在您的 index.html 文件中链接 CSS 文件

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

    假设您的文件夹结构是:

    project root 
         ├── server.js
         ├── package.json 
         └── public   
           ├── css  
             └── style.css
           ├── index.html
    

    【讨论】:

    • 正确!我错过了创建公用文件夹。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-26
    相关资源
    最近更新 更多