Express.js

Aus ahrensburg.city
Zur Navigation springen Zur Suche springen

Expres.js ist ein Webanwendungsframework für Node.js. Es ist Open Source und wurde von TJ Holowaychuk entwickelt. Express.js ist das beliebteste Node.js-Framework und wird von vielen Entwicklern verwendet.

Installation

Um Express.js zu installieren, führen Sie den folgenden Befehl aus:

mkdir express-app
cd express-app
npm init
npm install express

Beispiel

Hier ist ein einfaches Beispiel für eine Express.js-Anwendung:

const express = require('express');
const fs = require('fs');
const path = '/tmp/express.sock';

const app = express();

// Entfernen Sie den Socket, falls er bereits existiert
if (fs.existsSync(path)) {
    fs.unlinkSync(path);
}

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

app.listen(path, () => {
    fs.chmodSync(path, '777'); // Optional: Setzen Sie die Berechtigungen für den Socket
    console.log(`Example app listening at unix:${path}`);
});
your-project/
├── models/
│   ├── user.js
│   └── product.js
├── controllers/
│   ├── userController.js
│   └── productController.js
├── views/
│   ├── index.pug
│   └── users.pug
├── routes/
│   ├── users.js
│   └── products.js
├── app.js
└── package.json