Reputation: 23
I want to use JQuery so I did like this.
But it still doesn't work.
and this is .ejs page
<html>
<head>
<script src="/js/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="css/style.css">
<%
$(".btn").click(function(){
window.location.href = "/main";
});
%>
</head>
<body>
//display something
</body>
</html>
and this is app.js
var express = require('express');
var $ = require('jquery')(window);
var app = express();
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.engine('ejs', require('ejs').renderFile);
var server = app.listen(8080);
app.use(express.static('public'));
app.use('/js', express.static(__dirname + '/node_modules/jquery/dist'));
var router = require("./routes/index.js")(app);
the error code is,
$ is not defined
how can I fix it?
Upvotes: 0
Views: 3612
Reputation: 23
It did like this. node.js is so confusing. thank you so much.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<link rel="stylesheet" type="text/css" href="css/style.css">
<script>
$(".btn").click(function(){
window.location.href = "/main";
});
</script>
Upvotes: 0
Reputation: 2029
It looks like you're requiring the wrong file, since javascript is case sensitive.
You're requiring jquery
when you want to be requiring jQuery
. Try changing that.
Upvotes: 0
Reputation: 931
I am personally of the opinion that jQuery is best handled on the client side - it's better for your server's performance, otherwise you will have to work with the DOM on the server side - which will use more server resource.
However if you still want to use jQuery on the server side, then this link has a more complete guide: https://www.npmjs.com/package/jQuery
Upvotes: 1