Reputation: 3097
Have just started to learn nodejs.
Have few clarifications to be cleared:
Have installed Node js in Ubuntu from this link: https://dmitripavlutin.com/install-node-like-a-boss-with-nvm/
I understand that 'express' is a nodejs framework, and need to get the understanding of what exactly is 'express-generator'. Is the same like express?
From the above link, or from most of the links, its been said that nodejs is to be installed in the root or home directory(Ubuntu), Why only in this location?
I need to have my custom project, in /var/www/html/NodeDemo/ folder, how do i proceed:
To my understanding, it is like below:
UPDATE:
cd /var/www/html/NodeDemo
npm install -g express
I get:
/root/.nvm/versions/node/v7.7.3/lib
`-- [email protected]
In the same directory, if i try express app
I get express is not installed
How to start? Are the above steps correct?
Upvotes: 1
Views: 99
Reputation: 111268
1) Have installed Node js in Ubuntu from this link
nvm
is a Node version manager that allows you to install multiple versions of Node at the same time and quickly switch between them. But there is some magic that's going on, it does some things behind the scenes that may be confusing if you don't know what's going on. If you only need one Node version at a time then it may be more straightforward to install it normally using either from sources or using the official binary package. See this tutorial that I wrote for more info. It's about Node 6.7.0 but you can use any other version, just change the number. It shows how to install Node from sources and from binary packages, both as the only version and installing multiple Node versions at the same time, but not using any magic so everything is always where you put it.
See those answers for more info and examples:
2) I understand that 'express' is a nodejs framework, and need to get the understanding of what exactly is 'express-generator'. Is the same like express?
Express is a framework, something like a library that allows you to write Web applications in Node, RESTful APIs etc. It is similar to Hapi, Restify, Loopback, Koa.
Express generator is a program that writes a simple application that uses Express for your, so that you have all the boilerplate written for you and you can start from it. You don't need to use it, you can easily write everything from scratch - which is better if you want to learn because you will know all of your source code when you write it yourself. And it's not that hard. See thoss answers for some examples:
3) From the above link, or from most of the links, its been said that nodejs is to be installed in the root or home directory(Ubuntu), Why only in this location?
You can install Node anywhere you want. I often install Node in directories like $HOME/opt/node-v7.0.0
to test multiple versions and I don't even need to use sudo
for make install
that way. It's completely up to you where you install it. See my answer to point (1) for details.
4) I need to have my custom project, in /var/www/html/NodeDemo/ folder, how do i proceed
You can also go into a new directory and run:
npm init
and create a blank Node project that way (it will create only package.json
file). Then you can run:
npm install express --save
to install the Express framework in the node_module
directory and to add express
to dependencies in package.json
. Then you can just write a simple app.js
file that includes something like:
let app = require('express')();
app.get('/', (req, res) => {
res.end('Hello');
});
app.listen(3333, () => console.log('Listening on http://localhost:3333/'));
and have a simple Express app ready to go bu running:
node app.js
5) How do i proceed to start the server and run a sample program in Ubuntu.
See this answer:
Better if i get any link for CRUD program.
There are some good tutorials online:
Upvotes: 1