Warning: preg_grep(): Compilation failed: quantifier does not follow a repeatable item at offset 29 in /var/www/tg-me/post.php on line 75
Node.JS [ru] | Серверный JavaScript | Telegram Webview: we_use_js/4118 -
Telegram Group & Telegram Channel
👩‍💻 Создание простого сервера для сокращения 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



tg-me.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


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/we_use_js/4118

View MORE
Open in Telegram


Node JS ru | Серверный JavaScript Telegram | DID YOU KNOW?

Date: |

The lead from Wall Street offers little clarity as the major averages opened lower on Friday and then bounced back and forth across the unchanged line, finally finishing mixed and little changed.The Dow added 33.18 points or 0.10 percent to finish at 34,798.00, while the NASDAQ eased 4.54 points or 0.03 percent to close at 15,047.70 and the S&P 500 rose 6.50 points or 0.15 percent to end at 4,455.48. For the week, the Dow rose 0.6 percent, the NASDAQ added 0.1 percent and the S&P gained 0.5 percent.The lackluster performance on Wall Street came on uncertainty about the outlook for the markets following recent volatility.

Node JS ru | Серверный JavaScript from us


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