nodejs hello world example - symbol lookup error - javascript

UPDATE -- LINUX FEDORA 15
Following an example from:
http://simonwillison.net/2009/Nov/23/node/
My code:
var util = require('util'),
http = require('http');
http.createServer(function(req, res) {
res.sendHeader(200, {'Content-Type': 'text/html' });
res.sendBody('<h1>Hello World</h1>');
res.finish();
}).listen(8080);
util.puts('Server running at http://127.0.0.1:8080');
Produces the following error:
[abu#Beelzebub node_projects]$ nodejs helloworld.js
Server running at http://127.0.0.1:8080
nodejs: symbol lookup error: nodejs: undefined symbol: _ZN2v82V816IdleNotificationEv

this is 2009 tutorial and old api. You should do it like this
var http = require('http');
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World\n");
});
server.listen(8000);
console.log("Server running at http://127.0.0.1:8000/");
Your tutorial is old :) switch to this ->
http://howtonode.org/hello-node

To execute a node.js application, call it using node, not nodejs.
node helloworld.js
The particular error seems similar to a V8 build mismatch problem that was in Node 0.6.15. Have you tried using a newer (or rolling back to an older) version of Node?

To perform node.js installation on Fedora Linux download and install the standalone rpm (http://nodejs.tchol.org/stable/f16/SRPMS/repoview/nodejs.html) and perform install as follows:
Remove any existing node and nodejs applications using your package manager
Install node.js from standalone rpm
rpm –ivh
./configure
make
make install
Attempting to use a package manager may lead to dependency issues as described on the following site:
http://nodejs.tchol.org/

Related

Why we need http module installed to run our node js application?

I have found so many sources for now when the first application shows this line
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World!');
}).listen(8080);
Just being geek, my Question is why we need server/port to listen our requests for our node js applications?
Why can't we run as localhost/application_name instead?
Why we need that?
Can anyone elobarate please?
Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js' package ecosystem, npm, is the largest ecosystem of open source libraries in the world.
So if you want an application which only work with bash you don't need any http modules.
Browsers use HTTP. So if you want to develop a web application you need to use that protocol. If you run your project on 80 port you can use it like localhost/my_application.
Simple app.js
var result = doSomething();
functions doSomething(){
return "This the result";
}
console.log(result);
You can call it from bash. node app.js. But it just work and stop.
But if you want to serve this structure to WWW (which is using HTTP) you need to create server. http is a great and simple module for creating servers with node.js.
You can use other js files with using require.
app.js
var result = doSomething();
functions doSomething(){
return "This the result";
}
module.exports = result;
server.js
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
var result = require('app.js');
res.end(result);
}).listen(80);
Now you can run your server. node server.js
You can run arbitrary javascript with node. The code you've provided specifically sets up an http server that listens on port 8080. You can reach that webserver from a browser on the same computer by browsing to http://localhost:8080.
We don't need to install 'http' module in order to use it, it is already there in nodejs framework itself.
If you want to see output of any programming language you serve it as http because you want your browser to reach your server. Like what you do it php built in server php -S localhost:8081 or serve it via nginx or apache
If you don't serve your JS, PHP, Python ... over http, browser will treat those files as other unsupported file like a .tar file.
Node is JavaScript environment, Not a web server. You need a server to serve your application. You may use http, https or you can create any other server that can serve your js file.
Well, I do not know if my answer is clear enough to explain but hope you will have some idea why you use http module in your nodejs application.

How to run a website developed with node.js in local?

I would like to run a website developed with node.js in local.
I already installed node.js but when I lauch a .js file on my terminal, nothing happen ( $ node file.js )
Also, I guess I have to simulate a server ? How can I do that with node?
You can start a simple server with the example that can be found on nodejs.org:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');
https://nodejs.org/en/about/
To develop a website it is very helpful to use a web framework such as Express.
http://expressjs.com/
You should use:
npm start file.js
but also be sure to check out nodemon, which is very helpful for debugging - it restarts your app on code change.
Also be sure to check out the express generator, which will set up a node+express app that you can check out to figure how to get the server and routes going.

how to run node.js on windows with apache server installed in?

I'm a node.js begginer . Let's say I have an apache server(XAAMP) and node.js installed in C:\Program Files\nodejs\nodejs.exe on windows 7.
How can I run node.js in my apache server to simulate my code?
I mean, I know how to write node.js code but what I don't know how it's work on my server?
Apache server don't need for Node.js.
For create your own Node.js server:
Download and install Node.js
Create file hello.js:
var http = require("http");
var server = http.createServer().listen(3000); // beter way for create
server.on("request", function(req, res){
res.writeHead(200, {"Content-Type": "text/plain"});
// for view at page http://localhost:3000
res.write("Hello world");
res.end();
});
server.on("listening", function(){
// for view in console
console.log("Listen: 3000...");
});
In terminal go to dir where file hello.js and type:
node hello.js
Open your browser and point it at http://localhost:3000/. This should display a web page that says:
Hello world
A basic HTTP server
Node.js Manual & Documentation
If you like to work with a replacement for XAAMP you should finally take a look at MEAN.io.
At NpmJS.org you will find different solutions for most of your needs.
and like Reagan Gallant commented you should take a look at this famous stackoverflow post (if you need ideas).
NodeSchool indeed is a good entry point for your fist steps. After that npmjs will make sense and finally you will love Mean.io
You just make it use a different port than Apache is using (for example port 3000 which is the default for express-js and others) -- that is assuming that you don't need the two to work together.
If you do need them to work together, you add a forwarding module to Apache and configure the forwarding in Apache of certain URL to go to your local port for node-js

node.js script works off command line but not from browser?

I launched and amazon ec2 instance running ubuntu 12.04
I then followed the instructions to install node.js from here http://howtonode.org/how-to-install-nodejs
sudo apt-get install g++ curl libssl-dev apache2-utils
sudo apt-get install git-core
git clone git://github.com/ry/node.git
cd node
./configure
make
sudo make install
I then used the example code to make hello.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Node.js\n');
}).listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');
I then ran hello.js
/var/www$ node hello.js
Server running at http://127.0.0.1:8124/
However, when I try to access this from the url, with http://ec2-***.compute-1.amazonaws.com:8124/ I get an error page from my browser.
Any advice on how to get it to show up in the browser?
EDIT
I'm still encountering this problem after changing the above line of code
}).listen(8124, "127.0.0.1");
to this
}).listen(8124);
127.0.0.1 is the loopback address. Only the host can access it. If you want to listen on any available IP, just don't specify that parameter. Otherwise, specify the real IP you want to listen on.
I also had same issue, Uninstalling skype worked for me, You can also search for some setting which force skype to move to some other port.

How I can run this script in node.js?

I have install all modules need to run this script using CMD. When I am run this script in node.js through webmatrix:
var http = require('http');
var everyauth = require('everyauth');
var app = require('express').createServer();
app.get('/', function(request, response) {
response.send('Hello Express!!');
});
app.listen(2455);
It's not working. The error is:
iisnode encountered an error when processing the request.
HRESULT: 0x2
HTTP status: 500
HTTP reason: Internal Server Error
You are receiving this HTTP 200 response because system.webServer/iisnode/#devErrorsEnabled configuration setting is 'true'.
In addition to the log of stdout and stderr of the node.exe process, consider using debugging and ETW traces to further diagnose the problem.
The node.exe process has not written any information to the stdout or stderr.
I have tried with different different script from blog but they do not work. I tried Steven Sanderson's template in webmatrix and that does work.
To use your application with iisnode, you have to use port/socket supplied by iisnode (as in this case you're using IIS as a webserver instead of the one bundled in node).
Replace the last line with app.listen(process.env.port || 2455);, so that it will both work well with iisnode and be available on port 2455 when run with node.exe app.js.
Use
app.listen(process.env.port || 2455);
instead of `
app.listen(portno);
cause it's iisnode web server.

Categories

Resources