node.js won't read and display html file - javascript

I'm trying to read and display a basic html file with node.js, but i get no luck, since anything i do it returns the same error.
Here's the code
var http = require('http');
var fs = require('fs');
var url = require('url');
var port = 3000;
fs.readFile('./funckcionalnosti.html', function (err, html) {
if (err) {
throw err;
}
http.createServer(function (request, response) {
response.writeHeader(200, { "Content-Type": "text/html" });
response.write(html);
response.end();
}).listen(3000);
});
The error i get
I've read some things about node not dealing with temporary files, or that my file may not be within current script...i already tried moving files, tried _dirname and copying the entire path name, but it still won't work.
Any help please?

Related

How do I use Node functions and variables from HTML while using a server?

I'm trying to build a few programs that execute Node.js functions from HTML (e.g you press a button and some code using Node.js runs).
Here is the code I'm using to display the HTML
var http = require('http');
var url = require('url');
var fs = require('fs');
http.createServer(function (req, res) {
var q = url.parse(req.url, true);
var filename = "." + q.pathname;
fs.readFile(filename, function(err, data) {
if (err) {
res.writeHead(404, {'Content-Type': 'text/html'});
return res.end("404 Not Found");
}
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
return res.end();
});
}).listen(8080);
(Too long for a comment.)
First you must decide whether pressing a button shall
lead to a complete new HTML page that contains the results of the Node.js code execution or
update only parts of your existing HTML page.
In the first case, sending back static HTML pages (as your code does) will not be sufficient, you would need a template engine.
In the second case, you need client-side Javascript code to fetch JSON data (say) from your server and update the DOM of your HTML page.

Can't serve javascript file with html - node.js

I have a webpage with an HTML, CSS, and JS file. It also has one image. I am able to get all of them to function and serve onto the client side, except for the javascript portion. It is most likely a silly syntax thing but I have not been able to solve it.
Here is the code for my .js serving:
else if(req.url === '/index.js'){
console.log("SERVING JS")
res.writeHead(200, {'Content-Type': 'application/json'})
var javaContents = fs.readFileSync('./public/index.js', {encoding: 'UTF8'});
res.write(javaContents);
res.end();
}
^^^ Which is inside my requestHandler function:
function requestHandler(req, res){
}
Before this, I also have:
var http = require('http'), fs = require('fs');
var fs = require('fs');
And I declare my server variable last:
var server = http.createServer(requestHandler);
server.listen(9934, function(){
console.log("== Server is listening on port 9934");
});
I could post all my code but I think that is not necessary. Thank you!

(Node.js Basic) why put a period in front of pathname after parsing an url?

I just started Node.js and have been following a tutorial on Node.js in w3school.
var http = require('http');
var url = require('url');
var fs = require('fs');
http.createServer(function (req, res) {
var q = url.parse(req.url, true);
var filename = "." + q.pathname;
fs.readFile(filename, function(err, data) {
if (err) {
res.writeHead(404, {'Content-Type': 'text/html'});
return res.end("404 Not Found");
}
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
return res.end();
});
}).listen(8080);
I get the result fine. However, I didn't understand the dot(period) part.
In this tutorial, when you type the http://localhost:8080/summer.html on your browser, you are supposed to get summer.html. (summer.html is made previously) In the code, when I parse the url, and pathname it, I will get /summer.html , but why do I need a period in front? NodeJS will read ./summer.html?
Because of the answer, now I know that Node.js uses the same file location format as HTML. This question might help those who didn't know this.
. means "The current directory".
So ./summer.html means "summer.html in the current directory" while /summer.html means "summer.html in the root directory of the file system" (which probably doesn't exist).
A ./ in front of the URL is equivalent to the current path.
one more thing - . is the current directory and .. is the previous directory.
/ at the beginning of a URL means the root directory

error function is not defined for res node

I use the following code and when I run the program which is run this function I got error res is not defiend(TypeError: undefined is not a function),what It can be ?I have it in the function params???
http.createServer(function (req, res) {
res.redirect("http://localhost:3002");
}).listen(9006);
https://github.com/nodejitsu/node-http-proxy
There I use the
Setup a stand-alone proxy server with custom server logic
undefined is not a function means redirect is not a function (or method) of res. I'll bet you if you do console.log(res), you won't get an error, which means that, yes, res is defined, but redirect is not. It is an ExpressJS method, so I assume you haven't require'ed Express is your app, if you were planning to use it.
If you want to redirect without Express, one option is to set a different location header and response code (from here):
response.writeHead(302, {
'Location': 'your/404/path.html'
//add other headers here...
});
response.end();
From Wikipedia:
The HTTP response status code 302 Found is a common way of performing
URL redirection.
Edit
According to the library you've provided:
You may send a response page using what #Josh wrote or you may also handle the 404 page at the same time with the following code:
var http = require('http'),
fs = require('fs'),
util = require('util'),
url = require('url');
var server = http.createServer(function(req, res) {
if(url.parse(req.url).pathname == '/') {
res.writeHead(200, {'content-type': 'text/html'});
var rs = fs.createReadStream('index.html');
util.pump(rs, res);
} else {
res.writeHead(404, {'content-type': 'text/html'});
var rs = fs.createReadStream('404.html');
util.pump(rs, res);
}
});
server.listen(8080);
NodeJs don't have any redirect function use following code for redirect
res.writeHead(302, {
'Location': 'http://localhost:3002'
//add other headers here...
});
response.end();
Note TypeError: undefined is not a function means that function you trying to access is not defined.

Node.JS images not displaying to clients

I have server.js and client.html.
Server.js is running on nodejs and is simply:
var http = require('http'), fs = require('fs');
var app = http.createServer(function(request, response) {
fs.readFile("client.html", 'utf-8', function(error, data) {
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(data);
response.end();
});
}).listen(80);
and then I have client.html which is also very simply just
<img src="/public/images/avatar.gif">
Which just displays as though the image is not valid, I have checked the the director over and over and it is fine, why would it be doing this? I thought it might be because of the headers but text/html should surely display images?
Regards
Matt
It's not displaying the image because for every request (including the image request) it's returning the contents of clients.html.
If you want a static file server, i suggest looking at connect: http://www.senchalabs.org/connect/ or for something simpler, have a look at this: https://gist.github.com/rpflorence/701407

Categories

Resources