无法弄清楚如何访问 js 文件

Cannot figure out how to reach to a js file

我是网络开发的新手,我正在尝试 node.js 使用 express。

我的目录结构如下:

第一个应用程序
----node_modules
----public
--------脚本
--------------additems.js
----观看次数
--------home.ejs
----app.js

粗体是文件夹,斜体是文件。

这是文件 additem.js:

console.log("js connected");
alert("test");

这是 home.ejs 文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Test Node</title>
</head>

<body>

<h1>Dynamic Page</h1>
<p></p>

<ol id='olist'>Names List</ol>

<script type="text/javascript" src="../public/scripts/additems.js"></script>
<!-- <script type="text/javascript" src="/public/scripts/additems.js"></script>
<script type="text/javascript" src="/scripts/additems.js"></script>
<script type="text/javascript" src="/additems.js"></script>
<script type="text/javascript" src="additems.js"></script>
<script type="text/javascript" src="public/scripts/additems.js"></script> -->

<p>After Script tag</p>

</body>
</html>

这是 app.js 文件:

var express = require('express');
var app = express();

console.log("Server has been started!");

//app.use(express.static(__dirname + "/public"));

app.get("/", function(req,res){
    //res.send("Home Page");
     res.render("home.ejs");

 })

我可以看到服务器可以 运行 并显示 home.ejs 但没有迹象表明 additem.js 甚至可以触摸。

我错过了什么?

注意:我尝试了不同的脚本路径,并在 app.js 中尝试了 app.use 但没有成功,我也花了相当多的时间在网上搜索,我发现了类似的例子工作没有问题。

输出:

控制台: 服务器已启动!

浏览器:(没有出现警报

更新 1: 我尝试了 ../ 但它也不起作用。在 chrome 控制台中我可以看到这个错误: (X) Failed to load resource: the server responded with a status of 404 (Not Found) 但在添加 ..\ 之后,错误是: (X) GET http://localhost:3000/public/scripts/additems.js net::ERR_ABORTED

从 public 目录提供静态文件:

app.use(express.static(__dirname + "public"));

然后,public 现在将成为从浏览器访问这些文件时的根目录。

换句话说 public 将是 /

所以访问additems.js

<script type="text/javascript" src='/scripts/additems.js'></script>