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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With