将前端文件HTML,CSS和JS链接到后端node.js

问题描述 投票:0回答:1

我是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个回答
0
投票

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

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

假设您的文件夹结构为:

project root 
     ├── server.js
     ├── package.json 
     └── public   
       ├── css  
         └── style.css
       ├── index.html
© www.soinside.com 2019 - 2024. All rights reserved.