If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
तो जैसा कि आप पढ़ चुके हैं कि ExpressJS एक Node.js web application framework है जो हमें ready तो use functionality और tools provide कराता है , जिनकी help से हम development को efficient और fast बना सकते हैं। ExpressJS को आप simply NPM (Node Package Manager) के though easily install कर सकते हैं।
जिस तरह से PHP के लिए composer होता है बैसे ही NodeJs के लिए NPM. यह एक package manager है , इसकी help से आप external libraries install कर सकते हैं।
आपने NodeJS को पहले से install कर लिया है , अगर नहीं तो Install कर ले , इसके लिए आप NodeJS Installation Topic देख सकते हैं।
आगे बढ़ने से पहले हम express module को install कर लेते हैं।
npm install expressThat's it, यह command run करते ही ExpressJS install हो जायगा , अगर आप macOS or Linux पर काम कर रहे हैं तो sudo npm command का use करें।
sudo npm install expressअब सबसे पहला काम project directory create करना रहेगा , तो हम express-app नाम की एक directory बनाएंगे ।
> mkdir express-app
> cd express-appexpress-app में आने के बाद अब application को setup करेंगे, जिसके लिए npm init command का use किया जायेगा।
> npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help init` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (test) nodejs
version: (1.0.0)
description: for learning purpose
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to C:\Users\HP\Desktop\test\package.json:
{
"name": "nodejs",
"version": "1.0.0",
"author": "",
"license": "ISC"
}
Is this OK? (yes) yesnpm init command run करते ही आपसे package name , version , description , author etc info enter करने के लिए कहेगा , हालाँकि आप इन्हे skip करके आगे बढ़ सकते हैं।
तो अभी हमारा application setup हुआ है , अब हम index.js file बनाकर express का use करके server setup करेंगे।
// import express module.
const express = require('express');
const app = express();
const port = 8000;
// define route
app.get('/', function (req, res) {
res.send('Welcome to learnhindituts.com');
});
//listen server.
app.listen(port, function () {
console.log(`Server is running at http://localhost:${port}`);
});Explanation -
1. सबसे पहले Express module को load किया गया है। express एक function है जो Express app का instance return करता है।
const express = require('express'); 2. अब express() function को Express app का instance get करने के लिए call किया गया है।
const app = express();3. 3rd step में एक get() method के route define किया गया है ताकि इसे हम browser से access कर सकें।
app.get('/', function (req, res) {
res.send('Welcome to learnhindituts.com');
}); हालाँकि और भी request methods हैं जिन्हे आप Routing में पढ़ेंगे।
4. finally हमने Node.js को HTTP request को port 8000 पर listen करने के लिए instruct किया है। हालाँकि आप port change भी कर सकते हैं।
app.listen(port, function () {
console.log(`Server is running at http://localhost:${port}`);
});server run करने के लिए terminal open करें , project की root directory में जाकर नीचे दी गयी command को run करें।
node index.jscheck करने के लिए browser में http://localhost:8000 hit करें।