3

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?

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

5 Comments

This returning error "express is not defined", I need require any module?
I need define this static, before of my routes?
I would rather suggest you find this yourself. Play with it, try different options. That will help you understand how a node app works.
Okey, I will try this way! Thank you very much
This is working!! I read Express docs how to work this function, thank you very very much!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.