function2.jsで関数を呼び出すときにエラーが発生します。この関数は以下の単純なテストメソッドcreateStringです。私は私のdriver.htmlで関数を呼び出す必要があります。私は私のサーバーファイル(app.js)のjavascriptファイルを見つけるためにapp.getを使用しますが、私は ""を実行したときにエラーが発生します。 "http://localhost:3000/DRIVER/"を実行すると、driverというフォルダがありません。私はそれを使用するだけです。 jsフォルダはメインフォルダ内にあります。どのように私は急行路線で、HTMLでのjsフォルダを呼ぶのですエクスプレスルート経由でHTMLでJavascript関数を呼び出す
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)];
}
Driver.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!');
});
... JSファイルなどの静的コンテンツを提供するのが最善ですexpress.staticまだ
/js/function2.js
ルートを必要とされる "GETのhttp:// localhostを:3000 /ドライバ/のJS/function2.js" 'エラーではありません...エラーはどのようなステータスコードが返されたのか、なぜ失敗したのかの洞察を与えます。 –