最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

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

IT培训 admin 12浏览 0评论

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

我是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文件。它加载页面,但是没有样式,也没有加载图像...

为了使前端文件在被调用时可用,应该怎么做?

回答如下:

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

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

假设您的文件夹结构为:

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

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

我是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文件。它加载页面,但是没有样式,也没有加载图像...

为了使前端文件在被调用时可用,应该怎么做?

回答如下:

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

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

假设您的文件夹结构为:

project root 
     ├── server.js
     ├── package.json 
     └── public   
       ├── css  
         └── style.css
       ├── index.html
发布评论

评论列表 (0)

  1. 暂无评论