admin管理员组

文章数量:1418435

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?

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 edited Jan 30, 2015 at 11:32 Lai32290 asked Jan 30, 2015 at 11:26 Lai32290Lai32290 8,60819 gold badges70 silver badges104 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 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 remend to go through Expressjs docs to learn more about Express.

本文标签: javascriptHow to include JS file in HTMLusing NodeJS Express serverStack Overflow