【问题标题】:Calling Javascript function in HTML through express routes通过快速路由在 HTML 中调用 Javascript 函数
【发布时间】:2016-04-12 00:31:14
【问题描述】:

尝试在我的 function2.js 中调用函数时出现错误。该函数是一个简单的测试方法createString 下面。我需要在我的 driver.html 中调用该函数。我使用 app.get 在我的服务器文件 (app.js) 中查找 javascript 文件,但是当我运行“http://localhost:3000/DRIVER/”时,我不断收到“GET http://localhost:3000/DRIVER/js/function2.js”错误,没有名为驱动程序的文件夹。我只需要使用它。 js 文件夹就在主文件夹中。如何在快速路由中调用 html 中的 js 文件夹

function2.js

function createString() {
  var hash;
  var firstWord = randomWord();
  var secondWord = randomWord();
  var number = Math.floor(Math.random()*999)+1;

  hash = firstWord + number + secondWord;
  return hash.toString();
} 

function randomWord(){
  var words = ["Apple", "Apricot", "Avocado", "Banana", "Blackberry", "Blueberry", "Cherry", "Grapefruit", "Lemon", "Lime",
            "Coconut","Kiwi","Peach","Pear","Pineapple","Melon","Watermelon","Raspberry","Strawberry","Hanger",
            "Grape","Plum","London","Dublin","Moscow","Berlin","Madrid","Paris","Stockholm","Vienna",
            "Chair","Texas","California","Nevada","Florida","Montana","Bravo","Delta","Echo","Hotel",
            "Tango","Whiskey","Foxtrot","Golf","Zulu","Yankee","Magnet","Button","Watch","Red",
            "White","Green","Black","Yellow","Grey","Blue","Pink","Purple","Diary","Bottle",
            "Water","Fire","Wind","Sweet","Sugar","Stamp","Brush","Small","Medium","Large",
            "Brown","Piano","Guitar","Canvas","Carrot","Mouse","Dog","Cat","Squirrel","Truck",
            "Rabbit","Toothbrush","Chalk","Puddle","Elephant","Giraffe","Frog","Falcon","Eagle","Parrot",
            "Shark","Tiger","Butterfly","Turtle","Snake","Fish","Whale","Walrus","Kangaroo","Wolverine"];
  return words[(Math.floor(Math.random()*100)+0)];
}

驱动程序.html:

<!doctype html>

<html lang="en">
 <head>
 <meta charset="utf-8">

 <title>driver</title>
 <meta name="description" content="The HTML5 Herald">
 <meta name="author" content="SitePoint">

 <!--[if lt IE 9]>
 <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
 <![endif]-->
 </head>

 <body>
 <script src="js/function2.js"></script>
 <script>
    var session = createString();
    console.log(session);

    var url = window.location.href;
    console.log(url);

    var urlNew = url + "/" + session;
    console.log(urlNew);

    window.location.href = urlNew;
    location.replace(link);
    console.log(link);
 </script>
 </body>
 </html>

app.js:

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

//possible routes
app.get('/', function (req, res) {
  res.sendFile(path.join(__dirname+'/homepage.html'));
});

app.get('/DRIVER/', function (req, res) {
  res.sendFile(path.join(__dirname+'/driver.html'));
  console.log(req.url);
});

app.get('/NAV/', function (req, res) {
  res.sendFile(path.join(__dirname+'/homepage.html'));
  console.log(req.url);
});

var x = '+([A-Z])+([0-9])+([A-Z])+'

app.get('/DRIVER/'+x, function (req, res) {
  res.send('Hello Driver!');
  console.log(req.url);
});

app.get('/NAV/'+x, function (req, res) {
  res.send('Hello Navigator!');
  console.log(req.url);
});

// route to javascript
app.get('/js/function2', function (req, res) {
  res.sendFile(path.join('/js/function2.js'));
  console.log(req.url);
});

app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});

【问题讨论】:

  • "GET http://localhost:3000/DRIVER/js/function2.js" 不是错误...错误将是返回的状态代码,这将使我们了解它失败的原因。

标签: javascript html node.js express


【解决方案1】:

您必须为此添加 express.static

要提供静态文件,例如图片、CSS 文件和 JavaScript 文件, 使用 Express 中的 express.static 内置中间件函数。

在您的情况下,您必须在第一个应用程序之前添加。获取此行

app.use(express.static('public'));

您必须将 public 替换为您的文件夹

Serving static files in Express

【讨论】:

    【解决方案2】:

    app.get('/js/function2', function (req, res) : 在function2 之后没有.js

    &lt;script src="js/function2.js"&gt;&lt;/script&gt; 需要 /js/function2.js 路由

    然而 express.static 最好提供像 js 文件这样的静态内容...

    【讨论】:

      猜你喜欢
      • 2019-10-18
      • 1970-01-01
      • 1970-01-01
      • 2019-01-07
      • 2016-07-17
      • 2016-11-16
      • 1970-01-01
      • 2020-11-13
      • 1970-01-01
      相关资源
      最近更新 更多