似乎无法在Node.js服务器上看到图像
当服务器在Localhost上运行时的外观:300:
net/4j8c5.jpg“ rel =“ nofollow noreferrer”>
我非常非常这是新的,并试图制作一个视频游戏网站,以供人们购买东西。我想拥有游戏的图片,以便它可以使网页看起来更好,但是我似乎看不到服务器上的图片。在Chrome打开时,我可以在HTML文件上看到它们,但不能在Localhost上看到它们。我对此做了很多研究,似乎无法弄清楚。我有机会能够获得能够在服务器上显示图像的帮助吗?
html代码
<!DOCTYPE HTML>
<HTML>
<style type="text/css">
/* start of nav css*/
*{
margin: 0;
padding: 0;
}
h1 {
text-align: center;
color: #f00253;
padding: 20px;
}
.nav{
list-style-type: none;
display: -webkit-inline-flex;
background-color: #3f3d3d;
padding-left: 25%;
margin: 0;
width: 100%;
}
.nav li a {
color: white;
font-size: 18px;
padding: 20px 70px;
display: block;
text-decoration: none;
}
.nav li a:hover {
background-color: #f00253;
}
/* finish of nav css*/
</style>
<head>
<title>Jonathan's Database Game Store</title>
<link rel="stylesheet" type="text/css" href="nav.css">
</head>
<body>
<h1>Jonathan's Database Game Store</h1>
<ul class="nav">
<li><a href="#">Login</a></li>
<li><a href="#">PC Games</a></li>
<li><a href="#">Xbox Games</a></li>
<li><a href="#">Playstation Gmaes</a></li>
</ul>
<div class="left">
<h2>Raingbow Six Seige</h2>
<img src="r6.webp">
</div>
</body>
</html>
服务器。
const http = require('http')
const fs = require('fs')
const port = 3000
const server = http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/html'})
fs.readFile('home.html', function(error, data) {
if (error) {
res.writeHead(404)
res.write('Error: File Not Found')
} else {
res.write(data)
}
res.end()
})
})
server.listen(port, function(error){
if (error) {
console.log('something went wrong', error)
} else {
console.log('server is listening on port ' + port)
}
})
What it looks like when the server is running on localhost:300 :
What the HTML looks like itself:
I am very new to this and trying to make a video game website for people to purchase things. I wanted to have pictures of the game so that it can make the webpage look nicer but I can't seem to see the pictures on the server. I can see them on the HTML file when opened in chrome but not on localhost. I have done a lot of research on this and can't seem to figure it out. Is there any chance I can get some help on being able to get the images to show on the server?
HTML Code
<!DOCTYPE HTML>
<HTML>
<style type="text/css">
/* start of nav css*/
*{
margin: 0;
padding: 0;
}
h1 {
text-align: center;
color: #f00253;
padding: 20px;
}
.nav{
list-style-type: none;
display: -webkit-inline-flex;
background-color: #3f3d3d;
padding-left: 25%;
margin: 0;
width: 100%;
}
.nav li a {
color: white;
font-size: 18px;
padding: 20px 70px;
display: block;
text-decoration: none;
}
.nav li a:hover {
background-color: #f00253;
}
/* finish of nav css*/
</style>
<head>
<title>Jonathan's Database Game Store</title>
<link rel="stylesheet" type="text/css" href="nav.css">
</head>
<body>
<h1>Jonathan's Database Game Store</h1>
<ul class="nav">
<li><a href="#">Login</a></li>
<li><a href="#">PC Games</a></li>
<li><a href="#">Xbox Games</a></li>
<li><a href="#">Playstation Gmaes</a></li>
</ul>
<div class="left">
<h2>Raingbow Six Seige</h2>
<img src="r6.webp">
</div>
</body>
</html>
server.js Code
const http = require('http')
const fs = require('fs')
const port = 3000
const server = http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/html'})
fs.readFile('home.html', function(error, data) {
if (error) {
res.writeHead(404)
res.write('Error: File Not Found')
} else {
res.write(data)
}
res.end()
})
})
server.listen(port, function(error){
if (error) {
console.log('something went wrong', error)
} else {
console.log('server is listening on port ' + port)
}
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用Nodejs的HTTP模块制作HTTP服务器的问题
非常乏味,问题在于您没有在HTTP服务器上托管图像。
我建议
您使用 express ,这使一切变得更加容易。
使用
npm i Express
在控制台中安装它,然后您可以为HTTP服务器使用此代码The problem
Using NodeJS' HTTP module to make a HTTP server is pretty tedious, what the problem is, is that you aren't hosting the images on your HTTP server.
The fix
I would recommend that you use Express which makes everything way easier.
Install it with
npm i express
in console then you can use this code for your HTTP server我已经弄清楚了为什么蘑菇白痴的先前代码不起作用。在以下代码res.sendfile(__ dirname +“/pc.html”)中缺少__dirname +。这是与HTML和图像一起使用的最终代码。
I have figured out why Mushroom idiot's previous code did not work. was missing the __dirname + in the following code res.sendFile(__dirname + "/PC.html"). Here is the final code that works with the HTML and images.