Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jade+Express: Where does my client side Javascript go?

Where do I put my client side Javascript? I tried including it in my jade template like so:

script(type="type/javascript",src="../typeahead.js")

It returned this Cannot GET /typeahead.js

I also tried require('../typeahead.js') which unsurprisingly did not work. Just to clarify since I am new to Node.js, require is for server side code correct?

like image 719
Louis93 Avatar asked Sep 03 '26 05:09

Louis93


1 Answers

It can go wherever you want. You must write code to serve it.

Since you're using Express, there is a "static" module built-in for serving files on disk. Use it like this:

app.use(express.static(__dirname + '/public'));  // 'public'; is your directory for static files

Remember that with Node.js, you're generally writing the server. There are not set ways of doing things for web apps... there is no requirement that you're even building a web app. Everything it up to you. There are common modules for common tasks (such as Express/Connect) but in the end it's up to you.

require is part of the Node.js API and doesn't work in a browser. However, there is always Require.js and Browserify.

like image 105
Brad Avatar answered Sep 05 '26 22:09

Brad