Notice: file_put_contents(): Write of 1021 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 17405 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.4118
WE_USE_JS Telegram 4118
👩‍💻 Создание простого сервера для сокращения URL

Создайте простой сервер на Node.js с использованием Express, который принимает длинный URL и возвращает сокращённую версию. При переходе по сокращённому URL сервер должен перенаправлять на исходный URL.

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

npm init -y
npm install express nanoid

const express = require('express');
const { nanoid } = require('nanoid');

const app = express();
const port = 3000;

app.use(express.json());

const urlDatabase = {};

// Создание сокращенного URL
app.post('/shorten', (req, res) => {
const { url } = req.body;
if (!url) {
return res.status(400).json({ error: 'URL is required' });
}

const id = nanoid(6);
urlDatabase[id] = url;
res.json({ shortUrl: `http://localhost:${port}/r/${id}` });
});

// Перенаправление по сокращенному URL
app.get('/r/:id', (req, res) => {
const originalUrl = urlDatabase[
req.params.id];
if (originalUrl) {
res.redirect(originalUrl);
} else {
res.status(404).send('URL not found');
}
});

// Запуск сервера
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
Please open Telegram to view this post
VIEW IN TELEGRAM
👍4🤨1



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

👩‍💻 Создание простого сервера для сокращения URL

Создайте простой сервер на Node.js с использованием Express, который принимает длинный URL и возвращает сокращённую версию. При переходе по сокращённому URL сервер должен перенаправлять на исходный URL.

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

npm init -y
npm install express nanoid

const express = require('express');
const { nanoid } = require('nanoid');

const app = express();
const port = 3000;

app.use(express.json());

const urlDatabase = {};

// Создание сокращенного URL
app.post('/shorten', (req, res) => {
const { url } = req.body;
if (!url) {
return res.status(400).json({ error: 'URL is required' });
}

const id = nanoid(6);
urlDatabase[id] = url;
res.json({ shortUrl: `http://localhost:${port}/r/${id}` });
});

// Перенаправление по сокращенному URL
app.get('/r/:id', (req, res) => {
const originalUrl = urlDatabase[
req.params.id];
if (originalUrl) {
res.redirect(originalUrl);
} else {
res.status(404).send('URL not found');
}
});

// Запуск сервера
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});

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


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

View MORE
Open in Telegram


Telegram News

Date: |

In 2018, Telegram’s audience reached 200 million people, with 500,000 new users joining the messenger every day. It was launched for iOS on 14 August 2013 and Android on 20 October 2013. Telegram channels enable users to broadcast messages to multiple users simultaneously. Like on social media, users need to subscribe to your channel to get access to your content published by one or more administrators. But a Telegram statement also said: "Any requests related to political censorship or limiting human rights such as the rights to free speech or assembly are not and will not be considered." Although some crypto traders have moved toward screaming as a coping mechanism, several mental health experts call this therapy a pseudoscience. The crypto community finds its way to engage in one or the other way and share its feelings with other fellow members. Those being doxxed include outgoing Chief Executive Carrie Lam Cheng Yuet-ngor, Chung and police assistant commissioner Joe Chan Tung, who heads police's cyber security and technology crime bureau.
from us


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