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

Warning: file_put_contents(): Only 16384 of 17476 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.4093
WE_USE_JS Telegram 4093
👩‍💻 Создание простого сервера для статических файлов

Создайте 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
👍41



tgoop.com/we_use_js/4093
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/4093

View MORE
Open in Telegram


Telegram News

Date: |

Select “New Channel” Members can post their voice notes of themselves screaming. Interestingly, the group doesn’t allow to post anything else which might lead to an instant ban. As of now, there are more than 330 members in the group. How to Create a Private or Public Channel on Telegram? SUCK Channel Telegram To view your bio, click the Menu icon and select “View channel info.”
from us


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