I\'m trying to find out how to load and render a basic HTML file so I don\'t have to write code like:
response.write(\'...blahblahblah
...\
It's just really simple if you use pipe. The following is the server.js code snippet.
var http = require('http');
var fs = require('fs');
function onRequest(req, res){
console.log("USER MADE A REQUEST. " +req.url);
res.writeHead(200, {'Content-Type': 'text/html'});
var readStream = fs.createReadStream(__dirname + '/index.html','utf8');
/*include your html file and directory name instead of <<__dirname + '/index.html'>>*/
readStream.pipe(res);
}
http.createServer(onRequest).listen(7000);
console.log('Web Server is running...');
This is a pretty old question...but if your use case here is to simply send a particular HTML page to the browser on an ad hoc basis, I would use something simple like this:
var http = require('http')
, fs = require('fs');
var server = http.createServer(function(req, res){
var stream = fs.createReadStream('test.html');
stream.pipe(res);
});
server.listen(7000);
use app.get to get the html file. its simple!!
const express = require('express');
const app = new express();
app.get('/', function(request, response){
response.sendFile('absolutePathToYour/htmlPage.html');
});
its as simple as that.
For this use express module.
Install express: npm install express -g
This would probably be some what better since you will be streaming the file(s) rather than loading it all into memory like fs.readFile.
var http = require('http');
var fs = require('fs');
var path = require('path');
var ext = /[\w\d_-]+\.[\w\d]+$/;
http.createServer(function(req, res){
if (req.url === '/') {
res.writeHead(200, {'Content-Type': 'text/html'});
fs.createReadStream('index.html').pipe(res);
} else if (ext.test(req.url)) {
fs.exists(path.join(__dirname, req.url), function (exists) {
if (exists) {
res.writeHead(200, {'Content-Type': 'text/html'});
fs.createReadStream('index.html').pipe(res);
} else {
res.writeHead(404, {'Content-Type': 'text/html'});
fs.createReadStream('404.html').pipe(res);
});
} else {
// add a RESTful service
}
}).listen(8000);
I know this is an old question - here is a simple file server utility if you'd prefer to not use connect or express; but rather the http module.
var fileServer = require('./fileServer');
var http = require('http');
http.createServer(function(req, res) {
var file = __dirname + req.url;
if(req.url === '/') {
// serve index.html on root
file = __dirname + 'index.html'
}
// serve all other files echoed by index.html e.g. style.css
// callback is optional
fileServer(file, req, res, callback);
})
module.exports = function(file, req, res, callback) {
var fs = require('fs')
, ext = require('path').extname(file)
, type = ''
, fileExtensions = {
'html':'text/html',
'css':'text/css',
'js':'text/javascript',
'json':'application/json',
'png':'image/png',
'jpg':'image/jpg',
'wav':'audio/wav'
}
console.log('req '+req.url)
for(var i in fileExtensions) {
if(ext === i) {
type = fileExtensions[i]
break
}
}
fs.exists(file, function(exists) {
if(exists) {
res.writeHead(200, { 'Content-Type': type })
fs.createReadStream(file).pipe(res)
console.log('served '+req.url)
if(callback !== undefined) callback()
} else {
console.log(file,'file dne')
}
})
}
You can echo files manually using the fs object, but I'd recommend using the ExpressJS framework to make your life much easier.
...But if you insist on doing it the hard way:
var http = require('http');
var fs = require('fs');
http.createServer(function(req, res){
fs.readFile('test.html',function (err, data){
res.writeHead(200, {'Content-Type': 'text/html','Content-Length':data.length});
res.write(data);
res.end();
});
}).listen(8000);