ยป npm install express
Factsheet
Videos
For install Express in ubuntu via npm (Follow following steps)
First open terminal by 'ctrl + alt + t'
- Check nodejs is installed or not by command :- node -v
- Inter following path into terminal :- cd /var/www/html
- Create new folder (manually) or by command line :- mkdir nodeTest
- Enter in "nodeTest" folder :- cd nodeTest
- Create 'package.json' :- npm init //initialize node project (nodeTest)
- install Express :- sudo npm install express --save
Now open the 'nodeTest' folder from following path in your system :- /var/www/html/nodeTest
Now create :- index.js
index.js
var express = required("express");
var app = express();
app.get("/", function (req, res) {
res.send(" Welcome Node js ");
});
app.listen(8000, function () {
console.log("Node server is runing on port 8000...");
});
Runnig node server by terminal command :- node index.js
Now check url :- "localhost:8000"
As I said in my comment, ExpressJS it's a framework for developing servers. It's not a server by itself.
You should create a npm project (with npm init) and you can install it as a dependency with npm install express --save. Then refer to the "Hello World" example to see how to create a simple server: Hello World Starter
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
Also, you may want to serve static files by using
app.use(express.static('name_of_the_directory'));
ยป npm install expressjs
The save flag adds express to your package.json file. This file is used to store information about your project, such as its dependencies.
By doing npm install express --save, your project officially records that you have express installed and that your project needs it to run. Now lets say you sent your friend your source code, they can just run npm install and it will check package.json, recognize you installed express as a dependency, and it'll install it for them.
What is the --save option for npm install?
When installing a module using npm the --save option saves the module name and version to the package.json file. It is the default and the module will be saved event without the parameter. Having the modules listed in the package.json file allows you to install all the needed modules for a project using npm install
It should be mentioned that this saves a runtime dependency. If there is a module which is only used during development (e.g. nodemon) you install it using --save-dev so it is saved as a development dependency. Check the link above for all the npm options
Thank you all for your great responses, as they really allowed me to understand what is actually going on with the app.js file and where it receives it's functionality. Thank you to both Matthew Bakaitis and Bjarni Leifsson for their great input.
The only reason why I am going to go ahead and answer my own question is because while the nature of the app.js file was explained, exactly how to replicate calling the 'node app.js' command from the command line as to replicate a Node.js book that I was following wasn't implicitly addressed.
After searching google with the specific phrase "app.js in previous express.js versions", I happened upon a great article by Jilles Soeters entitled "Understanding the Express app.js":
http://jilles.me/getting-the-express-app-js/
Below is the excerpt of the solution that worked for me:
The file I'm covering is app.js, the main configuration file for your Express app. When I first opened app.js it confused me. I will save you the trouble of doing research and just cover them here.
Before you do anything add the following to your app.js
app.listen(3000);
You need that in order to be able to actual open your app in the browser. Go to 127.0.0.1:3000 after you've started your app (using node app.js)
After doing this, I was able to run the command
node app.js
I was able to run this command from the root directory of the Express install and proceed with my Node.js book with no additional problems.
This is a common problem that is caused when tutorials don't clearly explain what express is doing when it generates an app. You're trying to learn the new tech, but the tutorial is actively working against you. :(
The answer:
When you use the generator, package.json is configured so that npm start calls ./bin/www.
That file includes app.js and after the include, calls app.listen.
app.js doesn't call app.listen which is why if you call it directly, it exits with no code or info. You've got to call ./bin/www or you have to modify app.js...which then defeats some of the reasons you'd use a generator.
A related question here on the site saw a similar problem when trying to use supervisor to keep an app running but kept getting an exit 0 result.