last day (14 days later) » 

6:38 AM
0
A: Import JS modules in client side of a node.js app

krikaraDo this in your index.js: var module = require('module'); exports.index = function(req, res) { res.render('index', { module : module}); //Now your EJS will have the module }

 
So I have to put my file in node_modules? Imagine I want to import jQuery for instance.
 
Err, I don't think you want to import a jQuery module into the EJS file. If you want to import modules, my answer above works. If you are trying to use jQuery, you should stick with the <script src="../jquery.js">
 
What is EJS? What I want is to use jQuery on the client side.
The problem with <script src="../jquery.js"> is that the path is interpreted as localhost:3000/../jquery.js (the app is running on the port 3000), and the file is not found. I have to put the absolute path localhost/.../jquery.js which is dirty.
 
You're going to have to share more of your code. It should look something like this though : <script src="/javascripts/jquery-2.1.1.min.js"></script>
 
Here it is! :-)
 
6:38 AM
@Arnaud Okay so let me clarify something. <script src="localhost:3000//../jquery works, but <script src="../jquery doesn't work?
 
Hi
Yes that's it
 
I think it might have something to do with how you created the server
You never called createServer)app)
createServer(app)
 
yes that's true
do you have an example of the use of createServer please?
@krikara no sorry that is not true
 
var http = require('http');
 
localhost//../jquery works but not localhost:3000//../jquery
 
6:44 AM
okay
http.createServer(app).listen(..)
 
Ok so in my code I replace http by http.createServer(app) ?
 
You need to fix those 2 lines
the require and the http.listen line
THe problem is that you only created a listen server
 
there is still ET localhost:3000/jquery.js 404 (Not Found)
but
that's strange
I put a relative path
<script src="jquery.js"></script>
I am in localhost/myproject
And he don't look at localhost/myproject/jquery.js as I want
but at localhost:3000/jquery.js
 
What is going on?
Can you paste the exact line that works with Jquery?
 
I have done your changes and there is still localhost:3000/jquery.js 404 (Not Found)
 
6:49 AM
It shouldn't be localhost:3000
 
What works is <script src="http://localhost/fight/jquery.js"></script>
 
it should be folderName/jquery.js
try <script src="fight/jquery.js"></script>
 
What produces an error is <script src="jquery.js"></script>
I have not put localhost:3000 in my code
 
Sorry I mean <script src="fight/jquery.js"></script>. Does this one work?
 
no
GET localhost:3000/fight/jquery.js 404 (Not Found)
the problem is with the 3000
 
6:51 AM
can you post me the the line where it is http.createServer
 
http.createServer(app).listen(3000, function(){
console.log('listening on *:3000');
});
 
let me check something really fast
404 not found
 
yes
 
Doees localhost/fight/jquery.js work
 
yes
 
6:55 AM
That's really odd. Your server is supposed to on port 3000
 
2 additional pieces of information:
1) the address in my navigation bar is localhost:3000
2) don't forget app.get('/', function(req, res){
res.sendfile('index.html');
});
(I don't know if it is useful)
 
fight is your project folder, right?
 
yes
 
can you make a folder inside fight called javascripts
and put your jquery.js in there
 
done
and then?
 
6:57 AM
then do <script src="/javascripts/jquery.js"></script>
and let me know how that runs
 
but if you delete the port 3000, it works?
><
 
what do you mean?
It works if I put <script src="http://localhost/fight/javascripts/jquery.js"></script>
 
in your address bar
Does that work?
 
no
 
6:59 AM
oh...
 
oops sorry
there was not fight
yes it works
 
so it only works without the localhost ...
i mean without the port ...
 
yes
when I add :3000 in the address bar
Cannot GET /fight/javascripts/jquery.js
I get this message:
Maybe because of this : app.get('/', function(req, res){
res.sendfile('index.html');
});
 
oh maybe
Okay
under all the require s
put
app.set('port', process.env.PORT || 3000);
 
same error :'(
 
7:05 AM
I think your app.get res sendfile is doing it without a port maybe
 
if you see a solution that implies to change a lot of my code I am ready to do it
I have just begun this project...
 
Change this console.log('listening on *:3000'); to console.log('listening on *: app.get('port'));`
 
if I am right it is necessary to use a different port
 
port 3000 is fine
 
you mean this? console.log('listening on *: '+app.get('port'));
it says listening on 3000
 
7:07 AM
Sorry yes
It says it is listening on 3000 but the jquery isnt on 3000 lol...
Can you put the app.get('\', function
inside the http.createServer ?
I dont know if it works, but it seems like the problem is the res.send
 
the launching of the server works
but the same problem occurs
 
I think the issue is that app is res.sending your html file without consulting the server
because even if you dont start a server, the res send would work
 
maybe
I just took that from a baby example
 
instead of res.send
try res.render
 
like this?
app.get('/', function(req, res){
res.render('index.html');
});
inside createServer?
 
7:18 AM
you can pull the res.render outside of the server again
and put it in its original spot
and just to confirm something, does your html actually work? Like does it display what it is supposed to?
 
C:\wamp\www\fight>node index.js
listening on *: 3000
Error: Cannot find module 'html'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at new View (C:\wamp\www\fight\node_modules\express\lib\view.js:43:49)
at Function.app.render (C:\wamp\www\fight\node_modules\express\lib\applicati
on.js:493:12)
at ServerResponse.res.render (C:\wamp\www\fight\node_modules\express\lib\res
ponse.js:817:7)
before this error yes
 
errr
why can't it find the module http?
i mean html?
try just res.render('index');
 
let's start from the beginning
if you want to create a nodeJS app
that use jQuery on the client
what would you do?
I just want that
 
The way you configured your entire project
 
with just index:
 
7:22 AM
is not normal
 
I have another error
okay so
I would like to restart the project
because I have not coded anything for now
could you tell me how can I do?
 
Are you using eclipse or an IDE?
 
no
 
You should start there
download eclipse and then nodeclipse
click create new project and they will give u the basic layout of a project
 
I don't want to use eclipse
 
7:23 AM
Okay you can do it manually
but you should read up on the standard ways of using express
 
I am not sure if I really want to use express
 
it seems like you are reading the code of a very specific project and trying to expand on it
 
I just want something that works
the final purpose is to create a game
 
a project that was designed to be so simple that i can't expand
 
do you know another solution?
 
7:25 AM
because your initial code meant that only your computer can access it
Here's the thing, if you are planning on making a game, you're going to want to debug
It's better off to install an IDE so you can do that
I can't provide you a solution when I don't know exactly what you are trying to do. But I can you that your jquery is not working because of the way the code is written.
 
ok...
 
if you want your project to be able to be accessed from a different computer over a local network or the internet
You're going to need an entire project, not just 2 files
 
are you sure?
 
I mean, it really depends on what you're trying to do
but generally, App.js goes to index.js which renders for index.html
that's already 3 files there
your current program skips that step by sending an html file and not going through your server at all
 
ok I think I will learn node.js in more details
thank you for your help
 
7:35 AM
quick question, though
app.get('/', function(req, res){
res.render('index.html');
});
did you do that?
app.get('/', function(req, res){
res.render('index');
});
 

last day (14 days later) »