How to install node.js and create project in Eclipse - node.js

The steps I've tried:
1.(OK) install node from official website: https://nodejs.org/en/download/
Result: I'm able to open cmd(in any location, type node then use commands like "console.log" and it prints my messages)
2.(Failure) install express using npm install -g express from cmd gives me an error(picture attached
3.(OK) I've succeed installing express using the following command npm install express (without -g)
4.(OK) Writing a simple Hello World program works. Javascript file:
var http = require('http');
// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World\n");
});
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);
// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");
5.(Failure) However, I wanna run a bigger project, where besides one js file, I also have an index.html file. If I move both files to node installation directory, everything works. But I wanna be able to keep my projects somewhere else. If I try to run with node C:\Users\marius\Downloads\chat-example-master\indes.js I get the error: Cannot find module express. Thus it seems that when I installed express without "-g" I got it working only in node directory.(let me know if you have any doubt).
6.(Failure) When creating a Node.js project from Eclipse, I choose empty project, no template, then add a single and simple js file(the one with Hello World), right click on project name -> run as -> run Configuration -> Node Application -> New -> add my .js file -> Run. I get the following error:
Exception occurred executing command line.(steps from http://techprd.com/how-to-setup-node-js-project-in-eclipse/)
Cannot run program "node" (in directory "C:\Users\marius\workspace\FirstNodeProject"): CreateProcess error=2, The system cannot find the file specified
To recap: What I want is to be able to run node projects located anywhere with "node" in cmd and to create node.js and express project and run them from Eclipse.
Please let me know if you need more information.

Just to let others know if they come across this issue. I can run express apps from anywhere but in the root folder of every app I have to npm install express.
In Eclipse all you need to do is: Window->Preferences->Nodeclipse->uncheck "find .Node on PATH" and insert into Node.js path input your node.exe location (in my case: C:\Program Files\nodejs\node.exe)

Related

WebStorm debugging not stopping on breakpoints

My app is a node.js app which I can run through command line using: npm test inside the working directory. In WebStorm, I created a new configuration that looks like this:
Node interpreter: /usr/local/bin/npm
Node parameters: test
Working directory ~/dev/project
When I hit the run button, I get the correct output:
/usr/loca/bin/npm test
> app#1.0.0 test ~/dev/project
something else
Process finished with exit code 0
But since I have breakpoints set, it should have stopped on a breakpoint instead of getting to the Process finished part. I set my code to be pretty simple just so I can test breakpoints, so it looks like this:
"use strict";
let foo = false; (breakpoint)
let bar = true;
if (foo === bar) { (breakpoint)
console.log('something');
} else {
console.log('something else'); (breakpoint)
}
process.exit(1);
I also tried to make this work through command line. In my site settings, I set my Built-in server to port 12345. Can accept external connections, and allow unsigned requests.
when I run it through command line, I use:
npm --debug-brk=12345 test
I get the same result: it runs all the way to the exit point without stopping on the breakpoints.
Any ideas what I need to do to get this to work?
/usr/local/bin/npm is definitely not a Node interpreter, it's NPM package manager that is a Node.js application itself (i.e. it's run with Node.js interpreter). And test is a name of npm script, not a Node.js parameter.
If you like to debug your .js file that is run via npm test, you need to modify your npm script to include the debug options and then use NPM run configuration for debugging. Or, just create a Node.js configuration, set a valid path to Node.js execuitable there (/usr/bin/node or whatever it looks like on your system), then specify your .js file as JavaScript file:. See https://blog.jetbrains.com/webstorm/2017/09/debugging-node-js-apps-in-webstorm for more info

Determine & change DocumentRoot/port of node.js & run a function w/parameters

How do I determine and change both the "DocumentRoot" equivalent (of Apache) and port number on Node.js? I need to test a script file by calling the function and passing some parameters (yes, I know the file can execute it automatically).
There is no "getting started" or mention of this in the documentation.
Apache HTTPD is a generic web server. Node.js is a development framework that includes a standard library for creating HTTP servers. Thus, there isn't a standard configuration for a Node.js based application like there is for Apache HTTPD.
The basic example of writing a web server with Node.js is found at https://nodejs.org/api/synopsis.html#synopsis_example
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Which is to say, you define where files are loaded from and what port they are served over.
This is where frameworks like Fastify, Hapi, and Express come in. The make it easier to write generic web servers.
I first installed Node.js and it just has a command prompt in Windows. Still not sure the port number.
Node seems to execute scripts from it's directory (e.g. C:\Node.js\). As James mentioned in another answer Node.js doesn't do much on it's own. I followed a tutorial on getting Express to work on Windows. The tutorial failed to mention where scripts are run from so ignore the directions past running the following on the normal command prompt (not Node's console):
Run npm install
npm install express -g
npm install url -g
npm install fresh -g
npm install cookie -g
npm install methods -g
npm install crc -g
npm install send -g
npm install connect -g
npm install commander -g
npm i -D run-func
Okay, the last line of code allows us to run a function and pass parameters which I found via Pawel's answer here.
So now I can execute the following:
node run-func "C:\Users\John\HTTP\index.js" function_name param1 param2

Need help getting simple node.js express to run

I'm trying a simple sample of node express that I copied from online. The script is below which I think is pretty standard.
var http = require('http');
http.createServer(function(request, response)){
response.writeHead(200);
response.write("hello");
response.end();
}).listen(8080);
console.log('listening on port 8080...');
I used the bash on ubuntu on windows command as follows:
npm init
node SampleServer.js (the name of my file)
When I do this, I expect some response from the command line. But when I enter the "node SamplerServer.js" command, nothing happens. When I direct the browser to port 8080, I get an error message as well.
I'm using nodeclipse and the installing that on my machine was pretty complicated. Prior to any of the steps above, I created an express project in eclipse ide. It seems to perform a lot of pre steps but in the end, I think I'm getting some of the error messages below. I'm mentioning this because I'm thinking perhaps I installed one of the modules wrong.
enter image description here
Start with simple stuff...
Use express-generator to create simple app by the following command
1-> npm install -g express-generator with root or Admin access
2-> Run express demoapp.
3-> Navigate to demoapp
4-> Do npm install
5-> Run from command with npm start: it run by default on http://localhost:3000
Hit that URL from Browser

how to use the webodf editor in localhost with node.js

I did not find any tutorials to how to run the webodf i read his apis and source code i am getting how to start it can anybody share the idea.
- WebODF version 0.5.10-8-gf5949f3
-- Found Java: /usr/bin/java (found version "1.7.0.91")
-- external downloads will be stored/expected in: /home/peoplelink/build/downloads
-- Installed Node.js found: /usr/bin/nodejs - 0.10.25
-- good Node.js found: 0.10.25 (0.10.5 required.)
-- npm found: /usr/bin/npm
-- Android was not found: APK will not be built.
JS file dependencies were updated.
-- Configuring done
-- Generating done
-- Build files have been written to:
i got this but i am not getting to webodf.js file in it i am missed out anything.
I am not not sure what you currently have . But This is how you can configure an application using node.js to serve html files and view/edit odf files.
Lets Start with your Node.js Server
First Create an index.js file(name it whatever you want)in our application directory,then initialize node application using node init.
We will have following folder structure : -
./ document-editor
../app (our html code and libraries)
../index.js
../package.json
../And some other auto-generated files.
Include all the modules necessary.We are going to use Express,Multer and
other util libraries.
var express = require("express");
var multer = require('multer'); //for file handling
var util = require('util');
var app = express(); // init express app
Configure Routes and Html files to be served on user request to you server.
app.use(express.static('app')); // use this as resource directory
//APP ROUTING URL => FUNCTIONS
app.get('/', function (req, res) {
res.sendFile(__dirname + "/app/index.html");
});
// this means when we get a request on 'myAppContext/' url provide
index.html
Start the Server
//START THE SERVER
app.listen(3000, function () {
console.log("Listening on port 3000");
});
Note* : Make sure you have node.js envoirnment installed on your system before you start.
Now Lets Look at how we include webodf to our application.
First create a directory in your main folder(we name it 'app'), where all
the html,styles and scripts..etc will be stored.
/app (our html code and libraries)
../index.html
../script
..wodotexteditor-0.5.9(folder)
..myScript.js
../styles
../images
../And some other files.
Create an index.html file and include webodf and/or Editor JavaScript libraries(Contains Webodf included in build... so need to download separately).
Create a container element and local script necessary to run webodf editor.Make sure to add a odt file to directory for you test or you can use the one which comes with wodo-editor.
You can refer this link for creating a local webodf editor using wodo-text-editor and complete the above to steps(2 & 3).
After we have done the above things,we will go into our root directory and run 'node index' command.... and that's it folks.
Just hit localhost:3000/ and you will see a workable webodf editor.
I hope this helps to get started with node.js and webodf. I will soon create and full application with open/edit and save features using webodf and node.js.
Thanks :)

node.exe does not read file, but returns ellipsis (...)

End feb 2014 I downloaded node to c:\dev\0.10\ of my windows 7 machine, and node.exe opens fine.
Inspired by Smashing Node (some book), I want to achieve the following:
Firefox shows the plain text Smashing Node! if i point it to localhost:3000 and run in the node console
node my-web-server.js
where my-web-server.js file next to node.exe contains
require('http').createServer(function(req,res){res.writeHead(200, ('Content-Type', 'text/html'));res.end('<marquee>Smashing Node!</marquee>');}).listen(3000);;
but I fail: browser says
cannot connect with webserver on localhost:3000.
If paste the above oneliner in node it reacts with
{ domain: null,
_events: ..etc... }
Ignoring that, browser F5 results in Smashing Node!.
Node refuses the simplest of files, say have a file called hello.js next to node.exe and file contains the ascii text
console.log("hello");
i type:
node hello.js
node returns
...
(an ellipsis in dark gray)
Expected was: node returns hello
i type a file that does not exist like this:
node die
node returns
...
if i type
var http = require('http');
node:
undefined
(in a darkish gray) Expected was: something like ok, especially since the above oneliner resulted in a web server.
If however i type
npm install colors
node reacts with
npm should be run outside of the node repl, in your normal shell. (Press Control-D to exit.)
of course
node --version
also responds with an ellipsis.
What can i do?
How do i make node to process files?
You need to run node hello.js (and npm) on your command line shell (e.g. cmd.com or Windows PowerShell).
You are trying to run it in the Node REPL (node console), where you are expected to type only JavaScript.
It's hard to tell where the Smashing Node failed, particularly as a one-liner. The code runs OK on my machine, but you can split the code as follow and add a few console.log() to see how is executing:
console.log('about to start listening for requests');
require('http').createServer( function(req,res) {
console.log('a request was received', req.url);
res.writeHead(200, ('Content-Type', 'text/html'));
res.end('<marquee>Smashing Node!</marquee>');
}).listen(3000);;
console.log('listening for requests');
Save this code as my-web-server.js as you did before and then run from the command line "node my-web-server.js" and point your browser to localhost:3000
Also, I've got a very basic Node.js tutorial that might help you with the basics: http://hectorcorrea.com/#/blog/introduction-to-node-js/51

Resources