node.javascript进入本地主机
我正在尝试关注视频,但是当我在Web浏览器中通过本地主机加载时仍然无法获得。
我可以在 3000
上获得侦听控制台日志,但似乎这一行:
“ app.use(express.static)(“/users/under/name/desktop/weatherap app/public/app.html”);”);”不起作用。
有什么建议吗?
const express = require("express");
const app = express();
app.listen(3000, () => {
console.log("listening at 3000");
});
app.use(express.static("/Users/name/Desktop/Weather App/public/app.html"));
这是IM现在使用的代码。
server.js
const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.sendFile(
"/Users/name/Desktop/Weather App/public/app.html"
// "/Users/name/Desktop/Weather App/public/style.css"
);
});
// serve any HTML files located in /Users/name/Desktop/Weather App/public
// app.use(express.static("/Users/name/Desktop/Weather App/public"));
app.listen(3000, () => {
console.log("listening at 3000");
});
app.html
<!DOCTYPE html>
<html>
<head>
<title>Weather App</title>
</head>
<body>
<h1>Weather App</h1>
<div id ="container">
<p>
Place: <span id = "places"></span><br/><br/>
Temperature: <span id="temperature"></span>°C<br/><br/>
Feels like: <span id="feels"></span>°C<br/><br/>
Minimum Temp: <span id="min"></span>°C<br/><br/>
Maximum Temp: <span id="max"></span>°C<br/><br/>
Humidty: <span id="hum"></span>%<br/>
</p>
<div>
<input id="inputter" type="text" ></input><br/><br/>
<button id="entButton">Click here for weather forecast</button><br/><br/>
<button id="geoEnter">Click here for fast weather</button><br/>
</div>
</div>
<div>
</div>
<script href="/Users/name/Desktop/Weather App/server.js"></script>
<script src="/Users/name/Desktop/Weather App/public/app.js" ></script>
<link href="/Users/name/Desktop/Weather App/public/style.css" rel="stylesheet" type="text/css"/>
</body>
</html>
有什么建议吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只想
app.html
显示http:// localhost:3000
是URL,则可以执行此操作:如果您在
中有更多文件/users/name/desktop/weatherap/public
您要在要求时自动使用给客户,然后您可以添加此信息:因此,如果
styles.css.css
位于<代码>/用户/名称/桌面/天气应用/public ,那么/styles.css
的URL将自动为文件/users/users/name/desktop/weather App服务/public/styles.css
。更改
app.html中的链接
页面:这些标签中的URL需要相对于您
express.stext.static.static()
file中指定的目录从/
开始,因此它们独立于包含的页面URL。If you just want
app.html
to show whenhttp://localhost:3000
is the URL, then you can do this:If you have more files in
/Users/name/Desktop/Weather App/public
that you want to automatically serve to the client when requested, then you can add this:So, if
styles.css
was located in/Users/name/Desktop/Weather App/public
, then a URL for/styles.css
would automatically serve the file/Users/name/Desktop/Weather App/public/styles.css
.Change the links in your
app.html
page to this:The URLs in these tags need to be relative to the directory specified in your
express.static()
file and will usually start with a/
so they are independent of the containing page URL.提供给
express.station
的文件系统路径太长。express.static
采用目录路径参数,而不是文件路径。如果静态服务器端点的获取请求不包括末端的文件名(例如“.../app.html
)Express static Mifdware extrat Mifdredware for Afferable Mifdrendare for可配置的默认文件名称(最初设置为<代码> index.html ),请参见文档,尤其是
index
和扩展
选项对象的属性。The file system path supplied to
express.static
is too long.express.static
takes a directory path argument, not a file path. If the get request to the static server endpoint does not include a filename on the end (e.g. ".../app.html
) express static middleware looks for a configurable default file name (initially set toindex.html
) for content to serve.See also express.static documentation and in particular the
index
andextensions
properties of the options object.