Server API
Public
javascript
about 1 month ago19 views
javascript
const http = require('http');
const url = require('url');

const PORT = 3000;

const data = {
  info: {
    name: "Belmondo",
    aka: ["Belmondo", "Belmondawg", "Młody G", "G", "Kolega od jebanka"],
    genre: "rap / trap / memerap / pop",
    origin: "Gdynia, Polska"
  },
  albums: [
    { title: "Hustle As Usual", year: 2021 }
  ],
  songs: [
    { title: "wte i wewte", album: "Hustle As Usual" }
  ]
};

const server = http.createServer((req, res) => {
  const parsedUrl = url.parse(req.url, true);
  const { pathname } = parsedUrl;

  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
  res.setHeader('Access-Control-Allow-Headers', 'Content-Type');

  if (req.method === 'OPTIONS') {
    res.writeHead(204);
    res.end();
    return;
  }

  res.setHeader('Content-Type', 'application/json');

  if (pathname === '/info') {
    res.writeHead(200);
    res.end(JSON.stringify(data.info));
  } else if (pathname === '/albums') {
    res.writeHead(200);
    res.end(JSON.stringify(data.albums));
  } else if (pathname === '/songs') {
    res.writeHead(200);
    res.end(JSON.stringify(data.songs));
  } else {
    res.writeHead(404);
    res.end(JSON.stringify({ error: 'Not found' }));
  }
});

server.listen(PORT, () => {
  console.log(`✅ działa na http://localhost:${PORT}`);
});
This is a public paste that anyone can view.
Share