Node Js how to get started - node.js

I'm a newbie JavaScript programmer who wants to experiment with server side JavaScript using Node.Js on my Mac
I downloaded NodeJs and the wizard installed it, but now I can't see it in my applications?
1) How do I open it and start using it? Do I do it from the command line? if so, how do I start?
2) Can I connect it to a MySQL database I have installed with MAMP?
Thanks if you can help

To start node, you open a terminal and type:
/path/to/node scriptname.js
Depending on how you installed node, the actual location of node may vary. You should figure that out. You should probably add that path to your PATH environment variable. If you do this then you can simply type
node scriptname.js
There are plenty of samples around the net. As for mysql connectivity, check this out.

I am in pretty much in the same boat as you are, and I highly recommend you to watch the meetup talk by Ryan Dahl on http://nodejs.org/
It is only about an hour long, but it pretty shows you how you can use it for your own non-blocking applications.
In terms of driver for accessing SQL, you will need to install NPM http://npmjs.org/
You can do a quick search http://search.npmjs.org/, on the support for MySQL. Because node.js is still relatively new to the scene, you will see quite a bit of choices competing to meet your goal

Related

Shipping Postgres inside a node server [duplicate]

If it's possible, I'm interested in being able to embed a PostgreSQL database, similar to sqllite. I've read that it's not possible. I'm no database expert though, so I want to hear from you.
Essentially I want PostgreSQL without all the configuration and installation. If it's possible, tell me how.
Run postgresql in a background process.
Start a separate thread in your application that would start a postgresql server in local mode either by binding it to localhost with some random free port or by using sockets (does windows support sockets?). That should be fairly easy, something like:
system("C:\Program Files\MyApplication\pgsql\postgres.exe -D C:\Documents and Settings\User\Local Settings\MyApplication\database -h 127.0.0.1 -p 12345");
and then just connect to 127.0.0.1:12345.
When your application quits, you can always send a SIGTERM to your thread and then wait a few seconds for postgresql to quit (ie join the thread).
PS: You can also use pg_ctl to control your "embedded" database, even without threads, just do a "pg_ctl start" (with appropriate options) when starting the application and "pg_ctl stop" when quitting it.
You cannot embed it, nor should you try.
For embedding you should use sqlite as you mentioned or firebird rdbms.
Unless you do a major rewrite of code, it is not possible to run Postgres "embedded". Either run it as a separate process or use something else. SQLite is an excellent choice. But there are others. MySQL has an embedded version. See it at http://mysql.com/oem/. Also several java choices, and Mac has Core Data you can write too. Hell, you can even use FoxPro. What OS you on and what services you need from the database?
You can't embed it as a in process type thing like sqlite etc, but you can easily embed it into your application setup using Inno setup at http://www.innosetup.org. Search their mailing list archive and you will find someone did most of the work for you and all you have to to is grab the zipped distro and you can easily have postgresql installed when the user installs your app. You can then use the pg_hba.conf file to restrict the server to local host only. Not a true embedded DB, but it would work.
PostgreSQL is intended to run as a stand-alone server; it's probably possible to embed it if you hack at it hard and long enough, but it would be much easier to just run it as intended in a separate process.
HSQLDB (http://hsqldb.org/) is another db which is easily embedded. Requires Java, but is an excellent and often-used choice for Java applications.
Anyone tried on Mac OS X:
http://pagesperso-orange.fr/bruno.gaufier/xhtml/prod_postgresql.xhtml
http://www.macosxguru.net/article.php?story=20041119135924825
(Of course sqlite would be my embedded db of choice as well)
Well, I know this is a very very very old post, but if anyone has nowadays this question, I would refer to:
You can use containers running Postgres. Here's a post that could be helpful, doing something along this line using R:
https://rsangole.netlify.app/post/2021/08/07/docker-based-rstudio-postgres/?utm_source=pocket_mylist
Take a look at duckdb https://duckdb.org/docs/installation/ It is relatively new and still needs to mature. But it works pretty much like an embedded database ("In-process, serverless"), with bindings for several languages (Python, R, Java, ...)

How to deploy a nodejs application to google cloud?

I'm trying to deploy a nodejs application to google cloud, I've posted several SO questions about the errors I've been getting and how to fix them. Let me illustrate my endeavours...
problem 1) I tried following the tutorial, the hello world app deployment. When I entered the command to deploy,
$ gcloud preview app deploy app.yaml --set-default
It gave me an error saying managed vms aren't available for your application's region. I contacted someone from Google, a technical support representative from google, said his name was Ling, and he said he "wasn't aware managed vms weren't supported in India" and he wanted me to point out where I found that information. So I sent him this, note one of the answers. OK.....
problem 2) So, I was stuck, didn't really know what to do. Searched "how to deploy nodejs app on google cloud" and found a video in which a google developer informed me that there are "two main ways to run your application in the cloud, infrastructure as a service and platform as a service". Running it on app engine was the platform as a service, and that was off the list so I went with my only remaining option. There is a tutorial, the bookshelf app tutorial, also by google, and the last part describes how to run your application on the compute engine. I tried to go through the tutorial, but when I entered the command
git push cloud 7-gce:master
to push my code to the repository, it's asking me for my username and password, It never mentioned that in the tutorial, I don't know what to enter. I found this question but I'm not sure it applies to my situation. So next I went through some tutorials suggested by Zachary Newman, and I wasn't able to follow the tutorial where they use the ubuntu because when they tell you to hello.js file, it's really weird because I had to ssh into the ubuntu image vm I created and everything was really slow, I'm not familiar with commands on linux so I had to look up how to "save and exit" after creating the hello.js file. I got the "web server" to run, it's the reverse proxy nginx server, but since I don't know what the APP_PRIVATE_IP_ADDRESS is, when I accessed the page, I could see the nginx page but not the "hello world" message that's supposed to display. Please see the Prerequisites heading to see what the tutorial was trying to teach me. OK....
problem 3) On the google developers console, once you log in, there is a navigation section on the left, there is a link to the "Cloud Launcher". In there I found a link to create a Virtual Machine with nodejs installed, it's created by Bitnami. I clicked it and my vm was running, I went to the external ip address provided, and saw a Congratulations (ugh...) page by Bitnami. I didn't really know what to do from there, so I posted another SO question. A bitnami developer told me I "need to copy your files to the remote repository, create the configuration files and after that you have to include that configuration in the configuration of Apache to serve the application". How?!?! He just posted a link as well to a bitnami wiki page. I used filezilla to connect to my vm, and I tried to upload my application files to the vm. I was able to upload everything except the node modules (they were taking too long to upload and for some reason the ftp client kept loosing the connection with the server after 10 minutes or so of uploading). So I thought I would just ssh into the instance and try to npm install my dependencies. But it gave me more errors...
problem 4) I don't know why this post was put on hold, but I found this. I can't find the exact page right now but I read somewhere in the google docs that we can't ssh into an instance as root/administrator by design, and that we have to perform some advanced configuration in order to be able to do this. THE PROBLEM is that I can't install the dependencies unless I ssh in as an administrator....:'( So I just uploaded my node modules with filezilla. Everything looks good to go, I ssh into my instance and I cd into my application directory and enter the command
node app.js
and almost as if to taunt me, it console logs out "connected to mongo...." and "Listening on port 3000.....". I get so excited, I think it's working.....
problem 4) The server never really started running I think, it wasn't able to connect to the mongodb database (another instance) that I was running, this was the command to connect to the mongodb instance in my app.js file
mongoose.connect('mongodb://****/myappdatabase');
where the **** indicates the external IP of my mongodb instances. After about 5 minutes, I get an error on the ssh browser
Error: connect ETIMEDOUT ****:27017
BUT WHO HAS TIME TO READ ALL OF THIS RIGHT?
I'm not a developer, I don't have much experience with programming (close to zero actually). I'm a hobbyist. I picked up a book on c programming by stephen g. kochan about 5 or 6 months ago, and since then, I've just been following tutorials on the internet, a programming in java book, and some reference material. I wanted to create a website.
I learned some html, css, javascript and jquery off of w3schools and went through some tutorials on creating a basic node server and serving some html pages, that's literally all my application does. That's all I want it to do.
I used the express framework for node, and mongoose to connect to my mongodb running locally. Socket.io for a little bit of networking. I created the website for fun and I would really like to host it and share it with my friends, I made it this far, it would be really embarrassing to quit now because I can't host the website. I went through a lot more than what I posted on this question by the way.
Now I would really appreciate some help, even some guidance as to what I need to know, maybe books I should read or documentation I should go through to become more familiar with website hosting would be great. I am going to offer this question up for a bounty in 2 days but until then, if there is anyone that went through this and would like to help for no personal gain of their own, your saving my life. I've been going at this for about 4 days with no luck. Thanks and let me know if there is anything you would like to know about my application. I mean seriously, how hard could it be, I'm doing something (or everything) terribly wrong.
Wow dude. I thought about flagging this, but you appear to genuinely want to learn to program and need help. And experienced coders teaching novice coders is what this site is all about. So, here we go.
First and foremost, Linux is your friend. Learn it. Use it. I don’t use Ubuntu, but you should. Use 14.04 because it’s a forgiving disto and comes cram-packed with things you need when you’re just learning.
Time to address your questions:
Problem 1: you are using a gcloud preview product. Those are awesome, but not for beginners because you don’t know what you’re doing. Just stand up an Ubuntu instance on an n1-standard and rock it. No bells. No whistles. Learn Ubuntu linux with the same passion that you taught yourself how to program a web site.
Problem 2: Don’t use git. I use git every day and it’s awesome, but you have a long way to go and git is hella confusing. Take your workspace, tar/gzip it and just ssh the whole shebang to cloud. Then unzip it and you’ll have your whole code right there on your server. Caveat: never do this professionally. But for someone just learning … small bites dude … small bites.
Problem 3: Don’t use Bitnami pre-built images. It robs you of the opportunity learning how to do this from scratch and that is an invaluable experience. Here are two commands you must learn:
apt-get update
apt-get install <insert thing you want to install>
That’s it. Not rocket science and you will discover the power of package management on linux.
Problem 4: You are over-thinking this. For real, learn apt-get. It will get you so far, you’ll curse yourself for not using it earlier. apt-get node. apt-get mongo. And slam it all on one instance. Is that what I do professionally? Hell no. But it’s all about baby steps when you combine dev-ops with programming and you need to take those steps.
Errata: You wanted someone to recommend a book or a website or guidance. All you need to know is already on Ubuntu. They are called man pages. It’s short for manual. You can buy all the books in the world off Amazon and watch infinite youtube pages. It won’t compare to simply maning commands. You gotta trust me on this.
In summary: Just create an Ubuntu instance. tar and gzip all your code and scp it to your instance. Learn apt-get and for real, you are set. You will totally destroy your instance many times. So just delete it, re-create it, and keep starting over. Each time, you will be wiser and smarter than the time before. I tutor novice developers all the time. You gotta start small and work your way up. Eventually you’ll understand why you’d want to use git. Why you want to scale. Why LXC is amazing. Why GCE’s managed instance and VMs are crazy baller, and why scripting your deployment is a best practice. But first you have to understand how to build an instance and get your code there in any way possible. You’ll grow from there. Good luck, buddy.

Is using 'forever' still the suggested approach to run nodejs as a linux/unix service?

In the past couple of years NodeJS became a major player in the server landscape - and I really find it hard to believe that there is no decent way to have nodejs run as a service on a linux box. On Windows we have iisnode - but for non Windows environments the forever package is suggested as the way to go - instead of a real solution.
Is there maybe a servicized version of nodejs out there that I could not locate?
There isn't a "servicized" version of Node.js available in the sense you are thinking. Keeping your Node application running (for example in the event of a fatal error) is up to you entirely.
As suggested in the first comment, this is fairly subjective, but really there are two big packages (and one or two alternative methods) for making a service out of your Node application. As you've mentioned, forever is a popular choice. If you've never taken a look at pm2, I suggest doing so, as it offers some services that forever does not. Alternatively, you could search for information on supervisord, which I've had success with in the past. Finally, daemonizing Node with upstart is something to look at if the others don't fit well for you.

Starting NodeJS - what code to look for?

We have a whiteboard application powered by NodeJS sitting on a 'cloud' server (rackspace cloud). I recently scaled our server up to accommodate for the anticipated traffic with our launch, and in the process it shut down NodeJS. We are launching our product in a few hours, and our NodeJS developer has gone for the day.
The whiteboard application is supposed to run at http://rayku.com:8001 (the port is opened). However, it's not working because the port isn't listening to anything with NodeJS shut down.
I honestly have no idea which js file to run in order to start NodeJS for the whiteboard. There are many js files in a 'whiteboard' folder. Do you know what type of code I should look for that might suggest it is the right one? Or, do you know what types of logs I can dig up that might point me in the right direction?
Much appreciated
Look for app.js, server.js or something similar. It isn't required but a lot of people use app.js from what I have seen.
node or nodejs is typically the command to start. You may need to set environmental variables or arguments depending on how the developer set things up.
Also make sure to run it with screen or forever so it doesn't quit when you log out.
For common code. createServer is probably what is used to open the server itself. That is consistent between the core libraries and a lot of the frameworks. There might be another file that loads the module prior to executing so running might not work.

Target domain of node.js

I am just wondering how node.js is compared to other frameworks. Is it possible to develop rich internet applications using node.js? How is it compared to java NIO?
In short I am looking for the target domain of Node.js
not sure why there are people voting to close this question when I think it's perfectly viable, Node.JS is a new server side framework that is still undergoing heavy development.
to answer your question may be a little difficult for myself as I know nothing of Java, but I know a little about Node and use it on a regular basis whilst it's under development.
Node JS is basically a framework built up of several components that are built for speed, such as Google's Javascript Engine (V8), it was originally designed for Google Chrome but released as an Open Source project.
Many developers have taken V8 and placed it on the server, combining it with custom libraries integrated into V8 to allow File I/O and network access.
So what is Node.JS
Node JS is basically Googles V8 javascript engine as the language platform, Mixed in with Lib Event, which is a technique of using 1 thread to perform multiple tasks by creating Events from the kernel.
The primary usage of Node it's it's networking functionality, Ryan has contributed a really powerful HTTP Library that has helped it take of with Web Services, which is what it's main intention is for.
Why do I use NodeJS
I like Node JS simply because it's easy, fast and very modular, being able to supply information such as Files, Images, Text to a web browser directly from the Servers Memory (RAM) in under 10 lines simply helps understand the power behind it.
For instance, Nearly every web-browser makes a request for favicon.ico, which is usually ~10KB, Now if i had 100 Requests per second and every request was requesting my favicon, my hard drive would have to locate that file, blocking all other reads in the mean time.
I can just load the data, store it within a variable and send it to every client much much faster then the traditional methods.
What's the best part about Node.JS
The best part about node.js personally is the concept, the idea of being able to search thousands of clients concurrently without blocking any other client is the drive behind the speed, every thing is speed motivated, hence Google V8, it's called V8 for a reason, Lib Event, it's removes the requirement for loads of threads, which can be heavy on resources.
Getting Started
I seems like you have not really had a play with Node.JS, and if you have not then is suggest you isntall it and have a play for a few days, Join there IRC Chat and speak to some of the guys over there, there is usually a member of the immediate team there that will help you.
You can simply install node.JS on Ubuntu like so (In Bash):
if you do not have git
sudo apt-get install git-core
install node JS:
cd /etc/
sudo git clone git://github.com/joyent/node.git
cd node
sudo ./configure
sudo make
sudo make install
to test make sure you have it installed
node --version
if you get the version your ready to go, go to your home directy
cd ~/
mkdir Nodes
cd Nodes/
create a simple file in you ~/Nodes Directory called test.js and start away, you can run the code lie so:
cd ~/Nodes
node test.js
I had written that small guide to setting NodeJS up not just for yourself but for others who may read this and would like to set things up.

Resources