0
私のjqueryライブラリをロードする際に問題が発生しましたので、ファイル 'server.js'を使用できます。今、私は 'localhost:port /'に 'home.html'というファイルを送る 'route.js'というファイルを持っています。 home.htmlが表示されますが、server.jsへのリンクと共にjquery cdn/libraryがありますが、route.jsというノードを実行すると、 '$'が定義されていないというエラーメッセージが表示され続けます私のjsファイルは、HTMLのロードが完了する前に実行されています。この問題を解決するにはどうすればよいですか?app.use sendFileを使用した後にjqueryライブラリをロードする
//server.jsあなたがでサーバーのコードをロードしようとしている
exports.path = require('path');
var express = require('express');
exports.app = express();
$(document).on('click', '.btn', sendSurvery);
function sendSurvey(){
var myQueryUrl = "http://localhost:10003/survey";
$.ajax({url: myQueryUrl, method: 'GET'});
}
//route.js
var library = require('../../server.js');
library.app.use(function(request, response){
// response.sendFile(__dirname + '/../public/home.html');
response.sendFile(library.path.resolve(__dirname + '/../public/home.html'));
})
library.app.get('/survey', function(request, response){
response.sendFile(library.path.resolve(__dirname + '/../public/survey.html'));
})
library.app.listen(10003, function(){
console.log('connected on 10003')
})
//home.html
<!DOCTYPE html>
<html>
<head>
<title>Friend Finder Home Page</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<div class="rowOne">
<div class="jumbotron col-lg-6">
<h1>Hello, world!</h1>
<p>Click the button NOW!</p>
<p><a class="btn btn-primary btn-lg" href="#" role="button">CLICK NOW!</a></p>
</div>
</div>
<script src="https://code.jquery.com/jquery.js"></script>
<script type="text/javascript" src="../../server.js"></script>
</body>
</html>
静的ファイルにjsコードをロードすることを意味します – henhen
HTMLファイルで行うのと同じ方法でjsファイルを提供できます:http://expressjs.com/en/starter/static-files.html –