Getting started with python web development and Node.js + Express to create a web server

Table of contents

1. Introduction to Node.js + Express Framework

2 Comparison of Node.js + Express and Python to create a web server

3 Create a web server example using Node.js + Express

3.1 Download and install Node.js + Express

3.2 Create a web server process using Node.js + Express


1. Introduction to Node.js + Express Framework

        Node.js + Express is a development stack commonly used to build web applications, where Node.js is the runtime environment and Express is a popular web application framework. Their combination makes it easier to build high-performance, scalable web applications. Here's an introduction to Node.js and Express:

  1. Node.js:

    • Node.js is a JavaScript runtime based on the Chrome V8 engine for building high-performance, scalable network applications.
    • Node.js supports a non-blocking, event-driven I/O model, which makes it ideal for handling concurrent requests, such as web servers.
    • Node.js allows you to use JavaScript on the server side, enabling both the front end and back end to speak the same language.
    • The Node.js ecosystem is very rich, with a large number of third-party modules and tools that can be used for various tasks.
  2. Express framework:

    • Express is a Node.js-based web application framework designed to simplify the process of building web applications.
    • It provides routing, middleware, template engine and other functions to make it easier for developers to build feature-rich web applications.
    • Express uses middleware to process requests and responses, which makes the processing logic modular and easy to maintain.
    • It is flexible, you can choose to add the modules and functions you need according to the needs of your project.
    • Express is open source and has active community support, allowing it to evolve and improve.

        Together, Node.js + Express enables developers to quickly build modern, high-performance web applications. It is suitable for a variety of use cases, from simple static web pages to complex single-page applications and RESTful APIs. With the asynchronous nature of Node.js and the convenience of Express, developers can easily handle concurrent requests, routing, middleware processing, database integration, and other tasks to create powerful and scalable applications.

2 Comparison of Node.js + Express and Python to create a web server

        Node.js + Express is more professional and powerful in building modern, complex web applications, especially in handling asynchronous operations and high concurrency.

        Python is more suitable for general programming tasks, as well as some simple web server needs.

        In detail from the following aspects:

  1. Programming language:

    • Node.js + Express: Use the JavaScript programming language.
    • Python: Use the Python programming language.
  2. environment:

    • Node.js + Express: Node.js is a runtime environment dedicated to building network applications, which has powerful asynchronous processing and event-driven features.
    • Python: Python is a general-purpose programming language that can also be used to build web applications, but may not be as specialized as Node.js when it comes to web development.
  3. Modules and Libraries:

    • Node.js + Express: Use the Express framework to build web applications, which provides rich features, including routing, middleware, template engine, etc.
    • Python: Use modules such as http.serverand socketserverto create simple HTTP servers, but when creating more complex applications, you may need to use other libraries or frameworks.
  4. grammar:

    • Node.js + Express: Using JavaScript's syntax and module system.
    • Python: Use Python's syntax and module system.
  5. ecosystem:

    • Node.js + Express: The Node.js ecosystem is very rich, and there are a large number of third-party modules and tools available, which are suitable for building various types of web applications.
    • Python: Python also has a strong ecosystem, but it may be relatively less when it comes to web development, especially when it comes to building large-scale web applications.
  6. Asynchronous processing:

    • Node.js + Express: Node.js is great for handling a lot of concurrent requests because of its event-driven and non-blocking I/O model.
    • Python: Python may be relatively weak at handling large numbers of concurrent requests, but for some simple use cases it can work well too.

3 Create a web server example using Node.js + Express

     Node.js + Express create a web server example:

3.1 Download and install Node.js + Express

  1. Install Node.js: First, make sure you have Node.js installed. You can download and install it from the official Node.js website https://nodejs.org/ icon-default.png?t=N7T8https://nodejs.org/ .

  2. Create Project Directory: Create a new folder as your project directory. Navigate to that directory at the command line and perform the following steps.

  3. Initialize the project: Open a command line and run the following command to initialize a new Node.js project. Follow the prompts to configure the project information.

npm init

     4  Install Express: Run the following command in the command line to install Express.

npm install express

3.2 Create a web server process using Node.js + Express

  1. Create files: Create the following files under the project directory.

    • index.html: HTML page
    • styles.css: CSS style sheet
    • script.js: JavaScript script
    • server.js: Express server
  2. index.html: In index.htmlthe file, write the HTML page.

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <link rel="stylesheet" href="styles.css">
      <title>Node.js + Express Example</title>
    </head>
    <body>
      <header>
        <h1>Welcome to Our Website</h1>
        <nav>
          <ul>
            <li><a href="/">Home</a></li>
            <li><a href="/about">About</a></li>
            <li><a href="/contact">Contact</a></li>
          </ul>
        </nav>
      </header>
      <main>
        <h2>Home Page</h2>
        <p>This is the home page of our website.</p>
      </main>
      <script src="script.js"></script>
    </body>
    </html>
    

    3. styles.css: In styles.cssthe file, add some style rules.

    body {
      font-family: Arial, sans-serif;
      margin: 0;
      padding: 0;
    }
    
    header {
      background-color: #333;
      color: #fff;
      padding: 1rem;
    }
    
    nav ul {
      list-style: none;
      display: flex;
    }
    
    nav li {
      margin-right: 1rem;
    }
    
    nav a {
      text-decoration: none;
      color: #fff;
    }
    
    main {
      padding: 2rem;
    }
    

    4. script.js: In script.jsthe file, add a simple JavaScript script.

    console.log("Hello from script.js");
    

    5. server.js: In server.jsthe file, add the following code to create an Express server.

    const express = require('express');
    const app = express();
    const port = 3000;
    
    // 静态资源托管
    app.use(express.static('public'));
    
    // 路由
    app.get('/', (req, res) => {
      res.sendFile(__dirname + '/index.html');
    });
    
    app.get('/about', (req, res) => {
      res.send('<h2>About Us</h2><p>We are a team of developers.</p>');
    });
    
    app.get('/contact', (req, res) => {
      res.send('<h2>Contact Us</h2><p>Email us at [email protected].</p>');
    });
    
    // 启动服务器
    app.listen(port, () => {
      console.log(`Server is running on http://localhost:${port}`);
    });
    

6. Run the server: Run the following command at the command line to start your static server.

node server.js

7.  Visit the web page: Open your browser and visit http://localhost:3000, you should be able to see the web page you created, including navigation, styles and javascript.

        In this example, server.jsthe file uses Express to create a web server that handles /requests for the root path and other routes. Not only the HTML you index.htmlsee in the file, but also simple responses for the /aboutand routes. /contactThis example shows how to create a web server with routing and styling. You can add more routes and functions as needed.  

Guess you like

Origin blog.csdn.net/qq_35831906/article/details/132578679