1

I want a file like index.html to be loaded when the server is created. When I execute the server.js using node, I send a response as text like this res.end("text"). But I want the index.html to load.

I tried to load it using sendFile() in app.get('/getFile') but when I type in the address bar, I get the text for all the urls..even for localhost:3000/getFile.

This is my server.js:

    (function(){

var http = require("http");
var express = require('express');
var app =  express();
var bodyParser = require('body-parser');
var path = require('path');

// app.use(express.static(__dirname));
app.use(bodyParser.json());
app.use(express.static(__dirname+'/views'));

var server = http.createServer(function(request, response) {
  response.end("text");

});

server.listen('3000');
console.log("Server is listening");

app.get('/getFile',function(request,response){
    // response.end('shi');
    response.sendFile(path.join('/index.html'));
})
})();
6
  • If you're using express you should place your static files in the static folder. So you don't need to preload any html file, and write the code to send back to clients. Commented Aug 1, 2016 at 13:02
  • Maybe it's trivial, but in your code sample the /getFile handler send back first.html and not index.html as you wrote in your question. Commented Aug 1, 2016 at 13:06
  • @MarioAlexandroSantini. can you please explain how to send the file to clients. I'm trying to send using the sendFile() but it shows the same text. I read that the content inside createServer() is executed every time a request is sent to server. So maybe thats why it's not showing the html file. This doesn't really seem to be it but I just want to make sure. And yeah it's first.html that I'm actually sending. I said index.html so others can understand that I want it to be shown first Commented Aug 1, 2016 at 13:49
  • sorry I'm confused, you want to send back the index.html file, and that is waht you got in the code when you call the url '/getFile'. But as you are using express you should just put your html files in the static folder, and pass this to express app as described in the documentation. As it look like you just need to serve a static file, not something dynamically build on request. Commented Aug 1, 2016 at 14:20
  • @MarioAlexandroSantini. thanks. i got your point. Commented Aug 2, 2016 at 5:39

3 Answers 3

1

Change the following in your code:

var server = http.createServer(function(request, response) {
    response.end("text");
});

to this:

var server = http.createServer(app);

Now, you can serve your static index.html file with this code:

app.get('/', function(req, res, next){
    // Serve the index.html file in the root directory of the website.
    res.sendFile(path.join('/index.html'));
});

I hope this helps. If you have any questions, let me know.

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

3 Comments

I didn't know you can send app as parameter
If it works for you, then please choose the answer as the correct one and let me know if it doesn't work for you :)
I get sendFile() is not a function error. Anyway, I used app.use(express.static) and loaded the html. But the angularjs and other controller javascript files aren't being executed. Can you help me with that?
0

EDITED

I just made a folder and wrote the following code and I have checked that this is working.

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

app.use(express.static(__dirname + '/public'));

app.get('/', function(req, res) {
    res.sendFile(__dirname + '/public/indexz.html');
});

app.listen(1339);
console.log('Open this link http://localhost:1337');

Steps

1 Copy the code given above in a new folder and name it whatever you want, name the file server.js

2 go to your cmd and propagate to location of your code and now npm install express

3 now type node server on console

4 open the link that is there on the console.

Note : Make sure there is folder name public and there is a file named indexz.html in there.

Directory Arrangement

Edited

Regarding proper client side files arrangement

You will have to keep all your files in public folder, first of all and attach them accordingly in your html document.

Example

<!-- Owl Carousel Assets -->
<link href="css/owl.carousel.css" rel="stylesheet">
<link href="css/owl.theme.css" rel="stylesheet">
<script src="js/jquery.min.js"></script>
<script src="angular.js"></script>
<script src="controller.js"></script>

and then within public folder you'll have folders named js and css and in root of the public folder your html files.

7 Comments

thanks. I tried it before but I keep getting res.sendFile() is not a function.
@Nobody in separate file just try this. pastebin.com/jbYUYZNs and make sure that express is installed, don't add anything else just try this. Attach the folder as well as I have forgotten to add that.
im getting the same error when I tried your code. By "make sure that express is installed", you mean install by npm install -g express right? And what folder have you forgotten to add?
Folder addition to server the static files that is what I have missed. I'll have to make a folder for you now it seems.
Dude I keep getting TypeError: res.sendFile is not a function . I got the same thing when I tried with your code. I have done the npm install express
|
0

Looks your issue is with all the static assets.

In application server like express you have 3 different kind of elements to serve:

  1. static content: this is all html, client side js, css, images and so on

  2. server side templates or views, are documents you assemble with some sort of templating library like handlebars or jade to produce html

  3. api that provide data in xml or more common json format

Your issue is how to serve a static part.

You should add to the static folder of your express app the folder where you build your angular app.

Not just the index.html you need the client side .js, .css and all images the page require.

UPDATE:

Here you could find the express documentation about static content.

UPDATE:

When you add a static folder to the express middleware, you should be able to access your file directly.

For example, if you have 2 files: $project/static/main.js and $project/static/js/my-lib.js, you should use the following urls:

http://127.0.0.1:3000/main.js
http://127.0.0.1:3000/js/my-lib.js

Considering you're executing the node http server on localhost on port 3000.

If you provide a specific path to access the static content, then you have to rewrite your url so.

If you use a line like:

app.use('/staticFolder', express.static('staticFolder'));

Than the urls to the mentioned files will be:

http://127.0.0.1:3000/staticFolder/main.js
http://127.0.0.1:3000/staticFolder/js/my-lib.js

Also pay attention to the path you provide to express.

You should give a proper path, and it's always safer to use absolute paths:

app.use(express.static(__dirname + 'staticFolder'));

or

app.use('/staticFolder', express.static(__dirname + 'staticFolder'));

2 Comments

how do i send even the javascript files that are needed for the html. i created a staticFolder with all the html and angularjs and controller.js. i did the app.use(express.static(__dirname+"/staticFolder")) but still the js files are not executed. I tried to send using sendFile(), but I get an error saying sendFile() is not a function.
Did you check on the browser if you download or not the js files? And did you check your js, css and so on have the proper path and name in the html file? You could check with curl the specific js file to see what happens.

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.