Notice: file_put_contents(): Write of 5044 bytes failed with errno=28 No space left on device in /var/www/tgoop/post.php on line 50

Warning: file_put_contents(): Only 12288 of 17332 bytes written, possibly out of free disk space in /var/www/tgoop/post.php on line 50
Node.JS [ru] | Серверный JavaScript@we_use_js P.4264
WE_USE_JS Telegram 4264
👩‍💻 Создание простого сервера для статических файлов

Создайте HTTP-сервер на Node.js, который раздаёт статические файлы из указанной папки. По умолчанию сервер должен обслуживать файлы из папки public и работать на порту 3000.

Создайте структуру папок:

project/
├── server.js
└── public/
└── index.html


Решение задачи🔽

Файл server.js:

const http = require('http');
const fs = require('fs');
const path = require('path');

const PORT = 3000;
const PUBLIC_DIR = path.join(__dirname, 'public');

const server = http.createServer((req, res) => {
let filePath = path.join(PUBLIC_DIR, req.url === '/' ? 'index.html' : req.url);

fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('404 Not Found');
} else {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(data);
}
});
});

server.listen(PORT, () => {
console.log(`Сервер запущен на http://localhost:${PORT}`);
});

Файл public/index.html (пример содержимого):

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Static Server</title>
</head>
<body>
<h1>Добро пожаловать на мой сервер!</h1>
</body>
</html>

node server.js
Please open Telegram to view this post
VIEW IN TELEGRAM
👍2



tgoop.com/we_use_js/4264
Create:
Last Update:

👩‍💻 Создание простого сервера для статических файлов

Создайте HTTP-сервер на Node.js, который раздаёт статические файлы из указанной папки. По умолчанию сервер должен обслуживать файлы из папки public и работать на порту 3000.

Создайте структуру папок:

project/
├── server.js
└── public/
└── index.html


Решение задачи🔽

Файл server.js:

const http = require('http');
const fs = require('fs');
const path = require('path');

const PORT = 3000;
const PUBLIC_DIR = path.join(__dirname, 'public');

const server = http.createServer((req, res) => {
let filePath = path.join(PUBLIC_DIR, req.url === '/' ? 'index.html' : req.url);

fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('404 Not Found');
} else {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(data);
}
});
});

server.listen(PORT, () => {
console.log(`Сервер запущен на http://localhost:${PORT}`);
});

Файл public/index.html (пример содержимого):

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Static Server</title>
</head>
<body>
<h1>Добро пожаловать на мой сервер!</h1>
</body>
</html>

node server.js

BY Node.JS [ru] | Серверный JavaScript


Share with your friend now:
tgoop.com/we_use_js/4264

View MORE
Open in Telegram


Telegram News

Date: |

For crypto enthusiasts, there was the “gm” app, a self-described “meme app” which only allowed users to greet each other with “gm,” or “good morning,” a common acronym thrown around on Crypto Twitter and Discord. But the gm app was shut down back in September after a hacker reportedly gained access to user data. To edit your name or bio, click the Menu icon and select “Manage Channel.” The channel also called on people to turn out for illegal assemblies and listed the things that participants should bring along with them, showing prior planning was in the works for riots. The messages also incited people to hurl toxic gas bombs at police and MTR stations, he added. Matt Hussey, editorial director at NEAR Protocol also responded to this news with “#meIRL”. Just as you search “Bear Market Screaming” in Telegram, you will see a Pepe frog yelling as the group’s featured image. Private channels are only accessible to subscribers and don’t appear in public searches. To join a private channel, you need to receive a link from the owner (administrator). A private channel is an excellent solution for companies and teams. You can also use this type of channel to write down personal notes, reflections, etc. By the way, you can make your private channel public at any moment.
from us


Telegram Node.JS [ru] | Серверный JavaScript
FROM American