Lernen Sie Webentwicklung und wissen nicht, wie Sie ein Node.js-Projekt starten sollen? Mach dir keine Sorgen, ich habe dich! Ich führe Sie in nur 5 Schritten durch die Erstellung Ihres ersten Backends mit Node.js und Express.js.
?️5 wichtige Schritte:
- Schritt 1: Richten Sie das Projekt ein
- Schritt 2: Organisieren Sie Ihre Ordner
- Schritt 3: Erstellen Sie die server.js-Datei
- Schritt 4: Routen erstellen
- Schritt 5: Führen Sie Ihr Backend aus
Schritt 1: Richten Sie das Projekt ein ?️
1. Node.js und npm installieren: Laden Sie Node.js von der offiziellen Website herunter und installieren Sie es. Es wird mit npm (Node Package Manager) geliefert, der Ihnen bei der Verwaltung von Paketen hilft.
2. Erstellen Sie einen Projektordner: Erstellen Sie einen Ordner für Ihr Projekt. Öffnen Sie das Terminal (oder die Eingabeaufforderung) und geben Sie Folgendes ein:
mkdir my-node-project cd my-node-project
3. Initialisieren Sie Ihr Projekt: Richten Sie im Ordner ein neues Node.js-Projekt ein, indem Sie Folgendes eingeben:
npm init
Dadurch wird eine package.json-Datei erstellt, in der alle Ihre Projektinformationen und Abhängigkeiten gespeichert sind. Drücken Sie einfach für jede Frage die Eingabetaste, wenn Sie sich nicht sicher sind.
4. Express.js installieren: Express.js ist ein Framework, das den Aufbau eines Backends erleichtert. Installieren Sie es, indem Sie Folgendes eingeben:
npm install express
Schritt 2: Organisieren Sie Ihre Ordner?
Ordnung ist wichtig! So können Sie Ihr Projekt strukturieren:
- server.js: Hier schreiben wir den Hauptservercode.
- Routen/: Speichern Sie Routendateien hier (wo Sie Webanfragen bearbeiten).
- Controller/: Speichercode, der die Logik für Routen verwaltet.
- Modelle/: Speichern Sie Datenbankmodelle, wenn Sie eine Datenbank verwenden (wird derzeit nicht benötigt).
Beispiel einer Ordnerstruktur:
my-node-project/ ├── routes/ ├── server.js ├── package.json └── node_modules/
Schritt 3: Erstellen Sie die server.js-Datei ?️
1. Erstellen Sie die Datei: Erstellen Sie in Ihrem Projektordner eine Datei mit dem Namen server.js. Dies wird der Einstiegspunkt Ihrer App sein.
2. Schreiben Sie Ihren ersten Node.js-Server:
const express = require('express'); // Importing express const app = express(); // Creating an express app // Create a route that sends a response when visiting the homepage app.get('/', (req, res) => { res.send('<h1 id="Hello-Express-js-Server">Hello, Express.js Server!</h1>'); }); // Set up the server to listen on port 3000 const port = 3000; app.listen(port, () => { console.log(`Server is running on port ${port}`); });
3. Testen Sie es: Starten Sie Ihren Server, indem Sie Folgendes eingeben:
node server.js
Öffnen Sie nun Ihren Webbrowser und gehen Sie zu http://localhost:3000. Sie sollten „Hallo, Express.js Server!“ sehen. auf dem Bildschirm!
Schritt 4: Routen erstellen?
Routen definieren, wie Ihr Server auf verschiedene URL-Anfragen reagiert.
1. Erstellen Sie eine Route: In der Datei server.js haben Sie bereits eine Route:
app.get('/', (req, res) => { res.send('Hello, Express.js Server!'); });
2. Weitere Routen hinzufügen: Fügen wir weitere Routen hinzu:
app.get('/about', (req, res) => { res.send('This is the about page'); }); app.get('/contact', (req, res) => { res.send('This is the contact page'); });
3. Testen Sie Ihre Routen: Gehen Sie nach dem Speichern zu Ihrem Browser und besuchen Sie diese URLs:
- http://localhost:3000/ – sollte „Hallo, Express.js Server!“ anzeigen
- http://localhost:3000/about – sollte „Dies ist die About-Seite“ anzeigen
- http://localhost:3000/contact – sollte „Dies ist die Kontaktseite“ anzeigen
Schritt 5: Führen Sie Ihr Backend aus?
Um Ihren Server am Laufen zu halten und Änderungen zu testen:
1. Starten Sie Ihren Server: Starten Sie Ihren Server erneut:
node server.js
2. Testen Sie es: Sie können die URLs in Ihrem Browser aufrufen oder ein Tool wie Postman verwenden, um Anfragen zu senden.
3. Halten Sie Ihren Server auf dem neuesten Stand: Sie können nodemon installieren, das den Server automatisch neu startet, wenn Sie den Code ändern:
npm install -g nodemon
Führen Sie nun anstelle von node server.js Folgendes aus:
nodemon server.js
Ausgabe?:
Wenn Sie http://localhost:3000/ besuchen, sehen Sie:
Hello, Express.js Server!
Wenn Sie http://localhost:3000/about besuchen, sehen Sie:
This is the about page
✅?Empfehlung:
Verwenden Sie diese druckbare Backend Developer Notion-Vorlage, um Ihren Fortschritt zu verfolgen!
Entwickler-Anfänger haben oft Schwierigkeiten bei der Auswahl des richtigen Tech-Stacks, was zu Zeitverschwendung und Motivationsverlust führt. Deshalb habe ich in Notion eine wunderschön gestaltete und sehr einfach zu verfolgende 6-monatige Backend-Entwickler-Roadmap erstellt, damit Sie Ihren Fortschritt verfolgen und an Ihren Zielen festhalten können._
Diese Roadmap:
- ?️ Bietet einen klaren Weg, um Verwirrung zu vermeiden.
- ? Hilft Ihnen, motiviert zu bleiben, indem es Ihnen zeigt, wo Sie beginnen und enden sollen.
- ? Folgt einem strukturierten Plan, ähnlich einem Lehrplan.
- ? Organisiert Ihr Lernen mit wöchentlichen Zielen für Tools und Sprachen.
- ⏳ Stellt die Fertigstellung in 6 Monaten sicher und deckt alles ab, was Sie brauchen.
- ? Verfügt über ein schönes Design für eine einfache Navigation.
Vielen Dank für das Lesen dieses Artikels. Folgen Sie mir unbedingt auf ? für die neuesten Updates.
Read more: skills to become a backend developer in 6 months (roadmap)
The above is the detailed content of Your First Backend Application using Node.js. For more information, please follow other related articles on the PHP Chinese website!

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

10 fun jQuery game plugins to make your website more attractive and enhance user stickiness! While Flash is still the best software for developing casual web games, jQuery can also create surprising effects, and while not comparable to pure action Flash games, in some cases you can also have unexpected fun in your browser. jQuery tic toe game The "Hello world" of game programming now has a jQuery version. Source code jQuery Crazy Word Composition Game This is a fill-in-the-blank game, and it can produce some weird results due to not knowing the context of the word. Source code jQuery mine sweeping game

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

This tutorial demonstrates how to create a captivating parallax background effect using jQuery. We'll build a header banner with layered images that create a stunning visual depth. The updated plugin works with jQuery 1.6.4 and later. Download the

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

Matter.js is a 2D rigid body physics engine written in JavaScript. This library can help you easily simulate 2D physics in your browser. It provides many features, such as the ability to create rigid bodies and assign physical properties such as mass, area, or density. You can also simulate different types of collisions and forces, such as gravity friction. Matter.js supports all mainstream browsers. Additionally, it is suitable for mobile devices as it detects touches and is responsive. All of these features make it worth your time to learn how to use the engine, as this makes it easy to create a physics-based 2D game or simulation. In this tutorial, I will cover the basics of this library, including its installation and usage, and provide a

This article demonstrates how to automatically refresh a div's content every 5 seconds using jQuery and AJAX. The example fetches and displays the latest blog posts from an RSS feed, along with the last refresh timestamp. A loading image is optiona


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),