Join the Stack Overflow Community
Stack Overflow is a community of 6.9 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I'm creating a chat, using server Express of NodeJS and AngularJS for manager in client side

But when I try include /js/code.js in my html, it can not found, because is not routed by Express

<!-- my include in html -->
<script src="./js/code.js"></script> <!- this is not found in execution -->

Meu index.js:

var app = require('express')();
var http = require('http').Server(app);
var io = require("socket.io")(http);

app.get('/', function(request, response){
    response.sendFile(__dirname + '/index.html');
});

How to can I fix this problem, without route all js file I will using in my project or routing all js file in path a lot?

share|improve this question
up vote 4 down vote accepted

Use app.use to specify your public files to your node app, like below

app.use(express.static(yourPublicPath));

EDIT:

You are getting "Express undefined" error because it is not defined. You can easily fix this by defining your app in 2 stages:-

var express = require('express');
var app = express();

On a side note, I would strongly recommend to go through Expressjs docs to learn more about Express.

share|improve this answer
    
This returning error "express is not defined", I need require any module? – Lai32290 Jan 30 '15 at 11:43
    
I need define this static, before of my routes? – Lai32290 Jan 30 '15 at 11:48
1  
I would rather suggest you find this yourself. Play with it, try different options. That will help you understand how a node app works. – Abhishek Jain Jan 30 '15 at 11:51
    
Okey, I will try this way! Thank you very much – Lai32290 Jan 30 '15 at 12:01
    
No worries. Happy to help!! – Abhishek Jain Jan 30 '15 at 12:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.