Close Menu
Soshace Digital Blog

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    What's Hot
    Development

    Ensuring Quality: The Critical Role of Testing in Software Development

    Programming

    12. Уроки Node.js . Документация Http Модуля.

    Comics

    Tim&Tom JavaScript Frameworks

    Important Pages:
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Today's Picks:
    • Scaling Success: Monitoring Indexation of Programmatic SEO Content
    • Leveraging Influencers: Key Drivers in New Product Launches
    • How Privacy-First Marketing Will Transform the Industry Landscape
    • The Impact of Social Proof on Thought Leadership Marketing
    • Balancing Value-Driven Content and Promotional Messaging Strategies
    • Top Influencer Marketing Platforms to Explore in 2025
    • Emerging Trends in Marketing Automation and AI Tools for 2023
    • Strategies to Mitigate Duplicate Content in Programmatic SEO
    Thursday, December 18
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Soshace Digital Blog
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Services
    • SaaS & Tech

      Maximizing Efficiency: How SaaS Lowers IT Infrastructure Costs

      August 27, 2025

      Navigating Tomorrow: Innovations Shaping the Future of SaaS

      August 27, 2025

      Maximizing Impact: Strategies for SaaS & Technology Marketing

      August 27, 2025
    • AI & Automation

      Enhancing Customer Feedback Analysis Through AI Innovations

      August 27, 2025

      Navigating the Impact of AI on SEO and Search Rankings

      August 27, 2025

      5 Automation Hacks Every Home Service Business Needs to Know

      May 3, 2025
    • Finance & Fintech

      Critical Missteps in Finance Marketing: What to Avoid

      August 27, 2025

      Analyzing Future Fintech Marketing Trends: Insights Ahead

      August 27, 2025

      Navigating the Complex Landscape of Finance and Fintech Marketing

      August 27, 2025
    • Legal & Compliance

      Exploring Thought Leadership’s Impact on Legal Marketing

      August 27, 2025

      Maximizing LinkedIn: Strategies for Legal and Compliance Marketing

      August 27, 2025

      Why Transparency Matters in Legal Advertising Practices

      August 27, 2025
    • Medical Marketing

      Enhancing Online Reputation Management in Hospitals: A Guide

      August 27, 2025

      Analyzing Emerging Trends in Health and Medical Marketing

      August 27, 2025

      Exploring Innovative Content Ideas for Wellness Blogs and Clinics

      August 27, 2025
    • E-commerce & Retail

      Strategic Seasonal Campaign Concepts for Online and Retail Markets

      August 27, 2025

      Emerging Trends in E-commerce and Retail Marketing Strategies

      August 27, 2025

      Maximizing Revenue: The Advantages of Affiliate Marketing for E-Commerce

      August 27, 2025
    • Influencer & Community

      Leveraging Influencers: Key Drivers in New Product Launches

      August 27, 2025

      Top Influencer Marketing Platforms to Explore in 2025

      August 27, 2025

      Key Strategies for Successful Influencer Partnership Negotiations

      August 27, 2025
    • Content & Leadership

      The Impact of Social Proof on Thought Leadership Marketing

      August 27, 2025

      Balancing Value-Driven Content and Promotional Messaging Strategies

      August 27, 2025

      Analyzing Storytelling’s Impact on Content Marketing Effectiveness

      August 27, 2025
    • SEO & Analytics

      Scaling Success: Monitoring Indexation of Programmatic SEO Content

      August 27, 2025

      Strategies to Mitigate Duplicate Content in Programmatic SEO

      August 27, 2025

      Effective Data Visualization Techniques for SEO Reporting

      August 27, 2025
    • Marketing Trends

      How Privacy-First Marketing Will Transform the Industry Landscape

      August 27, 2025

      Emerging Trends in Marketing Automation and AI Tools for 2023

      August 27, 2025

      Maximizing ROI: Key Trends in Paid Social Advertising

      August 27, 2025
    Soshace Digital Blog
    Blog / Programming / 1. Express.js Lessons. Basics and Middleware. Part 1.
    Programming

    1. Express.js Lessons. Basics and Middleware. Part 1.

    Ivan RastvorovBy Ivan RastvorovNovember 23, 2016Updated:April 5, 2019No Comments5 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr Email Reddit
    1. Express.js Lessons. Basics and Middleware. Part 1.
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link

    expeess_p1

    Hey all! This and next articles will be devoted to various themes on development within the context of one app that we will consequently improve. This app is a web chat. Not a simple one, but the web chat that should demonstrate correct configuration, how to create common pages using Node.js and the Express framework, how to do templating, JSON service, authorization, chat, Socket, how to work with a database, to name just a few. The articles will contain materials created by using the framework version Express 3, the current version of Express 4. Out of date features of Express 3 are not used in the articles, so the only cardinal difference in Express 4 is that a number of libraries have been taken out of the framework – see Migrating from 3.x to 4.x. If you want to follow our lessons, we recommend:

    npm i express@3

    And your further transition to the Version 4 will be obvious.

    express

    So, let us start with Express technology, and we will get familiar with other aspects mentioned above while developing our chat.

    So, an empty directory. To create our own website, we will use the Express system :

    npm install i -g express@3.3.8

    Express is a Node.js framework that is mostly used for creating websites and online services. Let us install it globally because we will need a specialized Express utility. It enables to quickly generate the website structure, its main files

     express –help

    shows options. We will need:

    express –s  –e

    which means we will add both the support of sessions and the template engine --ejs to our new website. The way how it is included and works will be explored in the process. So, everything’s done.

    Read More:  Node.js Lesson 8: Inheritance from Errors, Error

    Open it in WebStorm. You can use any other editor convenient for you. So, look what’s been generated. App.js is a main file, and we will analyze its details very soon.

    /**
     * Module dependencies.
     */
    
    var express = require('express');
    var routes = require('./routes');
    var user = require('./routes/user');
    var http = require('http');
    var path = require('path');
    
    var app = express();
    
    // all environments
    app.set('port', process.env.PORT || 3000);
    app.set('views', __dirname + '/views');
    app.set('view engine', 'ejs');
    app.use(express.favicon());
    app.use(express.logger('dev'));
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(express.cookieParser('your secret here'));
    app.use(express.session());
    app.use(app.router);
    app.use(express.static(path.join(__dirname, 'public')));
    
    // development only
    if ('development' == app.get('env')) {
      app.use(express.errorHandler());
    }
    
    app.get('/', routes.index);
    app.get('/users', user.list);
    
    http.createServer(app).listen(app.get('port'), function(){
      console.log('Express server listening on port ' + app.get('port'));
    });
    

     Package.json–is a main file of the app. Add the title of our project there.

    {
      "name": "chat",
      "version": "0.0.1",
      "private": true,
      "scripts": {
        "start": "node app.js"
      },
      "dependencies": {
        "express": "3.3.8",
        "ejs": "*"
      }
    }

    To launch the project you need dependencies. So, we open the console again and enter:

     npm i 

    Everything contained in рackage.json will be installed in node_modules. So, a new directory appeared. All the modules needed for work have also been installed. In order to launch it, let us create the launch configuration аpp.js; put a tick inside Single instance only for your convinient restart. Launch it. Go to Chrome:

    http://localhost:3000/

    This is a port, where our app ‘listens‘ to something, and it works.

    Go back to the project now. There are a lot of things generated; to make it easier for us, let us only the vital things necessary for Express . These are:

    – to connect Express,
    – to create an app, which creates a function used for handling requests:

    var express = require('express');
    var app = express();

    Further add the http-server:

    http.createServer(app).listen(app.get('port'), function(){
      console.log('Express server listening on port ' + app.get('port'));
    });

    Respectively, Express will handle all incoming requests. We will also need http modules.

    var express = require('express');
    var http = require('http');
    var path = require('path');
    
    var app = express();
    
    http.createServer(app).listen(app.get('port'), function(){
      console.log('Express server listening on port ' + app.get('port'));
    });

    Aрp.get gets a value from a special hidden property of the object арp, which can be installed as follows (add to our app.js):

    app.set('port', 3000);

    Get all the rest commented out, we do not need these things.

    var express = require('express');
    var http = require('http');
    var path = require('path');
    
    var app = express();
    app.set('port', 3000);
    
    http.createServer(app).listen(app.get('port'), function(){
      console.log('Express server listening on port ' + app.get('port'));
    });

    Now, if we launch all these things, we will get a server listening on port 3000 and doing nothing. But why? Because the еxpress function doesn’t handle requests by default. To make it handle them, you need to add a special handler called Middleware  in Express  terms . It looks just the same as an average function, accepts req and res, transfers all objects and can do certain things:

    // Middleware  
    app.use(function(req, res, next) {  
        res.end("Hello");  
    });
    

    This function will respond in the same way on all requests. Launch it. Check. It works!

    Read More:  The Impact of Integrated Development Environments on Programming

    How does Middleware differ from an average handler on.request? It has the third parameter called  next. It serves to integrate Middleware into chains. For example, you’ve got one Middleware and do another Middleware below. First of them checks, whether it is

    if (req.url == '/') {

    it calls

    } else {

    Otherwise, it delivers the control further:

    next();
    
    

    We will continue soon, please stay tuned!  The lesson code can be found here.

    keep-calm-there-s-more-to-come-14

    The materials for this article have been borrowed from the following screencast.

    We are looking forward to meeting you on our website blog.soshace.com

     

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Ivan Rastvorov
    • Website

    Related Posts

    Mastering REST APIs: Essential Techniques for Programmers

    December 18, 2024

    Crafting Interactive User Interfaces Using JavaScript Techniques

    December 17, 2024

    Effective Strategies for Utilizing Frameworks in Web Development

    December 16, 2024
    Leave A Reply Cancel Reply

    You must be logged in to post a comment.

    Stay In Touch
    • Facebook
    • Twitter
    • Pinterest
    • Instagram
    • YouTube
    • Vimeo
    Don't Miss
    JavaScript June 22, 2020

    Create simple POS with React.js, Node.js, and MongoDB #7: Adding redux to other components

    This tutorial part serves as a continuation of the previous tutorial. In this tutorial part, we are going to integrate the redux to Register and Forget password component.

    Agile Software Development, Scrum part 3

    August 12, 2016

    Full List of JavaScript Conferences 2020 [41 Events] Updated 28.08.2020

    November 6, 2019

    Node.js Lesson 14: Asynchronous Development

    January 6, 2021

    Categories

    • AI & Automation
    • Angular
    • ASP.NET
    • AWS
    • B2B Leads
    • Beginners
    • Blogs
    • Business Growth
    • Case Studies
    • Comics
    • Consultation
    • Content & Leadership
    • CSS
    • Development
    • Django
    • E-commerce & Retail
    • Entrepreneurs
    • Entrepreneurship
    • Events
    • Express.js
    • Facebook Ads
    • Finance & Fintech
    • Flask
    • Flutter
    • Franchising
    • Funnel Strategy
    • Git
    • GraphQL
    • Home Services Marketing
    • Influencer & Community
    • Interview
    • Java
    • Java Spring
    • JavaScript
    • Job
    • Laravel
    • Lead Generation
    • Legal & Compliance
    • LinkedIn
    • Machine Learning
    • Marketing Trends
    • Medical Marketing
    • MSP Lead Generation
    • MSP Marketing
    • NestJS
    • Next.js
    • Node.js
    • Node.js Lessons
    • Paid Advertising
    • PHP
    • Podcasts
    • POS Tutorial
    • Programming
    • Programming
    • Python
    • React
    • React Lessons
    • React Native
    • React Native Lessons
    • Recruitment
    • Remote Job
    • SaaS & Tech
    • SEO & Analytics
    • Soshace
    • Startups
    • Swarm Intelligence
    • Tips
    • Trends
    • Vue
    • Wiki
    • WordPress
    Top Posts

    Maximizing LinkedIn: Top Tools for Lead Generation and Automation

    LinkedIn December 4, 2024

    How to Prepare for Your First Coding Job Interview?

    Interview January 23, 2020

    Уроки React. Урок 4. Домашнее Задание.

    Programming September 14, 2016

    Work With the Customer

    Tips December 31, 2015

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    About Us
    About Us

    Soshace Digital delivers comprehensive web design and development solutions tailored to your business objectives. Your website will be meticulously designed and developed by our team of seasoned professionals, who combine creative expertise with technical excellence to transform your vision into a high-impact, user-centric digital experience that elevates your brand and drives measurable results.

    7901 4th St N, Suite 28690
    Saint Petersburg, FL 33702-4305
    Phone: 1(877)SOSHACE

    Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
    Our Picks
    JavaScript

    Fortune 500 top hiring trends in 2019. How top companies attract best talents?

    Beginners

    Sorting Algorithms Overview: Theory and Visualization

    Comics

    Search of Investments

    Most Popular

    Safeguarding Innovation: A Guide to Startup Intellectual Property

    Startups

    20 JavaScript Interview Questions – Part 2

    Interview

    Web Development Newsletters: JavaScript, React, Vue, Angular Email Newsletters

    JavaScript
    © 2025 Soshace Digital.
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions

    Type above and press Enter to search. Press Esc to cancel.