Edupala

Comprehensive Full Stack Development Tutorial: Learn Ionic, Angular, React, React Native, and Node.js with JavaScript

Express Basic Route – c

const express = require('express');

const app = express();

app.get('/', (req, res) => {
  res.send('Hello World');
});

app.get('/contact', (req, res) => {
  res.send('<h1>Edupala.com</h1>');
});

app.get('/stocks/:name', (req, res) => {
  const stockName = req.params.name;
  res.send('<h1>' + stockName + '</h1>');
});

//Post method
app.post('/books', (res, res) => {
   res.send('Post request successfully');
})

app.listen(3000, function(){
  console.log('Server started on port 3000...');
});

We above example we can access localhost:3000 to root page and localhost:3000/contact to contact route, and we can pass user name as http://localhost:3000/users/edupala

Example 2 Accessing static html file

We can access static HTML file, in this file, we will create three file index.html, about.html and contact.html in public (create a public folder in root). Step 1: Add the following code in the app.js file

const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');

const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));

// Set Static path
app.use(express.static(path.join(__dirname, 'public')));

app.listen(3000, () => {
  console.log('Server started on port 3000...');
});

Step 2: Create a public folder and create three file index.html, about.html, and contact.html and add the following code in index.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Express static Home page</title>
</head>
<body>
  <ul>
    <li><a href="index.html">Home</a></li>
    <li><a href="about.html">About</a></li>
    <li><a href="contact.html">Contact</a></li>
  </ul>
  <div class="container">
    <h1>Home</h1>
    <p>This is the Home page</p>
  </div>
</body>
</html>

Accessing JSON data from URL in the express and downloading HTML file. Create folder downloads and add file download.pdf. Add the following code in app.js file.
Add the following code in about.html and same code in contact with change the about word in contact in case of contact.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Express static About page</title>
</head>
<body>
  <ul>
    <li><a href="index.html">Home</a></li>
    <li><a href="about.html">About</a></li>
    <li><a href="contact.html">Contact</a></li>
  </ul>
  <div class="container">
    <h1>About</h1>
    <p>This is the About page</p>
  </div>
</body>
</html>

Accessing JSON data from URL in express and downloading html file. Create folder downloads and add file download.pdf. Add following code in app.js file

const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');

const app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));

// Set Static path
app.use(express.static(path.join(__dirname, 'public')));

app.get('/users', (req, res) => {
  let users = [
    {
      first_name: "Will",
      last_name: "Smith",
      age: 54,
      gender: "male"
    },
    {
      first_name: "Arjun",
      last_name: "Reddy",
      age: 23,
      gender: "male"
    },
    {
      first_name: "Anitha",
      last_name: "Sharma",
      age: 18,
      gender: "female"
    }
  ];

  res.json(users);
});

app.get('/download', (req, res) => {
  res.download(path.join(__dirname, '/downloads/receipt.doc'));
});

app.listen(3000, () => {
  console.log('Server started on port 3000...');
});
Express Basic Route – c

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top