【问题标题】:routing a webpage error, nodejs and expressjs路由一个网页错误,nodejs和expressjs
【发布时间】:2021-08-16 12:42:32
【问题描述】:

我正在尝试使用 express.js 直接路由到 html 文件,遇到未知错误,对 express.js 不熟悉,我不知道如何解决这个问题:-

这里是 js 代码:-

const express = require('express');
const path = require();
const app = express();

const port=process.env.PORT || 8000;

// public static path
const static_path = path.join(__dirname,"../public");

app.use(express.static(static_path));


app.get("",(req,res)=>{
  res.send("welcome to this main page");
})

app.get("/about",(req,res)=>{
    res.send("welcome to this about page");
  })

  app.get("/weather",(req,res)=>{
    res.send("welcome to this weather page");
  })

app.get("*",(req,res)=>{
    res.send("404 Error page oops");
  })

app.listen(port,()=>{
    console.log(`listening to the port ${port}`);
})

静态网页:- HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Weather App</title>
    <link rel="stylesheet" href="css/style.css">
</head>
<body>
    <h1>Welcome to the static web</h1>
</body>
</html>

收到此错误:-

internal/validators.js:124
    throw new ERR_INVALID_ARG_TYPE(name, 'string', value);
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The "id" argument must be of type string. Received undefined

【问题讨论】:

    标签: node.js express routes backend


    【解决方案1】:

    好的,我使用“type nul > filename”命令在附加文件夹中创建新文件。当我直接使用创建文件的VSCode功能时,它现在工作正常,......不知道为什么,但我认为旧命令可能已经过期,告诉我我是否错了,但我刚刚解决了我的问题...... .

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-22
      • 1970-01-01
      • 2015-12-09
      • 1970-01-01
      • 2016-08-08
      • 1970-01-01
      相关资源
      最近更新 更多