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'));
})
})();

expressyou 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./getFilehandler send back first.html and not index.html as you wrote in your question.sendFile()but it shows the sametext. I read that the content insidecreateServer()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'sfirst.htmlthat I'm actually sending. I saidindex.htmlso others can understand that I want it to be shown first