node.javascript进入本地主机

发布于 2025-02-11 02:53:06 字数 2515 浏览 2 评论 0 原文

我正在尝试关注视频,但是当我在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>&degC<br/><br/>
      
      Feels like: <span id="feels"></span>&degC<br/><br/>
      
      Minimum Temp: <span id="min"></span>&degC<br/><br/>
      
      Maximum Temp: <span id="max"></span>&degC<br/><br/>
      
      Humidty: <span id="hum"></span>&percnt;<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>

有什么建议吗?

I'm trying to follow a video but still can't get when I load by local host in the web browser.

I am get a console log of listening at 3000 but it seems that this line:

"app.use(express.static("/Users/name/Desktop/Weather App/public/app.html"));" is not working.

Any suggestions?

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"));

This the code Im using now.

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>

any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘香 2025-02-18 02:53:06

如果您只想 app.html 显示 http:// localhost:3000 是URL,则可以执行此操作:

const express = require("express");
const app = express();

app.get("/", (req, res) => {
    res.sendFile("/Users/name/Desktop/Weather App/public/app.html");
});


app.listen(3000, () => {
    console.log("listening at 3000");
});

如果您在中有更多文件/users/name/desktop/weatherap/public 您要在要求时自动使用给客户,然后您可以添加此信息:

const express = require("express");
const app = express();

app.get("/", (req, res) => {
    res.sendFile("/Users/name/Desktop/Weather App/public/app.html");
});

// 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");
});

因此,如果 styles.css.css 位于<代码>/用户/名称/桌面/天气应用/public ,那么/styles.css 的URL将自动为文件/users/users/name/desktop/weather App服务/public/styles.css


更改 app.html中的链接页面:

<script src="/app.js"></script>
<link href="/style.css" rel="stylesheet" type="text/css"/>

这些标签中的URL需要相对于您 express.stext.static.static() file中指定的目录从/开始,因此它们独立于包含的页面URL。

If you just want app.html to show when http://localhost:3000 is the URL, then you can do this:

const express = require("express");
const app = express();

app.get("/", (req, res) => {
    res.sendFile("/Users/name/Desktop/Weather App/public/app.html");
});


app.listen(3000, () => {
    console.log("listening at 3000");
});

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:

const express = require("express");
const app = express();

app.get("/", (req, res) => {
    res.sendFile("/Users/name/Desktop/Weather App/public/app.html");
});

// 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");
});

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:

<script src="/app.js"></script>
<link href="/style.css" rel="stylesheet" type="text/css"/>

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.

自演自醉 2025-02-18 02:53:06

提供给 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 to index.html) for content to serve.

See also express.static documentation and in particular the index and extensions properties of the options object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文