1const express = require('express');
2const cors = require('cors')//cors for own server connected with own
3const app = express();
4require("dotenv").config();//dotenv config
5const port = process.env.PORT || 5000;
6
7//Middleware
8app.use(cors());
9app.use(express.json());
10
11app.get('/',(req,res) =>{
12 res.send('Server is ok')
13});
14
15app.listen(port, () =>{
16 console.log('Port is Ok');
17})
1const express = require('express')
2const cors = require('cors')
3const bodyParser = require('body-parser')
4require('dotenv').config()
5const ObjectId = require("mongodb").ObjectId;
6
7const app = express()
8app.use(cors())
9app.use(express.json())
10const port = process.env.PORT || 5000;
11
12app.get('/', (req, res)=>{
13 res.send('welcome to backend')
14})
15
16app.listen(port, () => {
17 console.log(`App listening at localhost:${port}`);
18})
1Express.js, or simply Express, is a web application framework for Node.js,
2released as free and open-source software under the MIT License.
3
4It is designed for building web applications and APIs.
5It has been called the de facto standard server framework for Node.js.
1# You can run the application generator with the npx command (available in Node.js 8.2.0).
2npx express-generator
3# For earlier Node versions, install the application generator as a global npm package and then launch it:
4npm install -g express-generator
5# For example, the following creates an Express app named myapp. The app will be created in a folder named myapp in the current working directory and the view engine will be set to Pug:
6express --view=pug myapp
7
8 create : myapp
9 create : myapp/package.json
10 create : myapp/app.js
11 create : myapp/public
12 create : myapp/public/javascripts
13 create : myapp/public/images
14 create : myapp/routes
15 create : myapp/routes/index.js
16 create : myapp/routes/users.js
17 create : myapp/public/stylesheets
18 create : myapp/public/stylesheets/style.css
19 create : myapp/views
20 create : myapp/views/index.pug
21 create : myapp/views/layout.pug
22 create : myapp/views/error.pug
23 create : myapp/bin
24 create : myapp/bin/www
25# Then install dependencies:
26cd myapp
27npm install
28# On MacOS or Linux, run the app with this command:
29DEBUG=myapp:* npm start
30# On Windows Command Prompt, use this command:
31set DEBUG=myapp:* & npm start
32# On Windows PowerShell, use this command:
33$env:DEBUG='myapp:*'; npm start
1npm install express
2npm install express --save
3Docs : https://expressjs.com/
4- Example API : https://jareer.xyz/ -
1const http = require('http');
2const app = require('./app');
3
4const normalizePort = val => {
5 const port = parseInt(val, 10);
6
7 if (isNaN(port)) {
8 return val;
9 }
10 if (port >= 0) {
11 return port;
12 }
13 return false;
14};
15const port = normalizePort(process.env.PORT || '3000');
16app.set('port', port);
17
18const errorHandler = error => {
19 if (error.syscall !== 'listen') {
20 throw error;
21 }
22 const address = server.address();
23 const bind = typeof address === 'string' ? 'pipe ' + address : 'port: ' + port;
24 switch (error.code) {
25 case 'EACCES':
26 console.error(bind + ' requires elevated privileges.');
27 process.exit(1);
28 break;
29 case 'EADDRINUSE':
30 console.error(bind + ' is already in use.');
31 process.exit(1);
32 break;
33 default:
34 throw error;
35 }
36};
37
38const server = http.createServer(app);
39
40server.on('error', errorHandler);
41server.on('listening', () => {
42 const address = server.address();
43 const bind = typeof address === 'string' ? 'pipe ' + address : 'port ' + port;
44 console.log('Listening on ' + bind);
45});
46
47server.listen(port);
48
49