Files
parcer/scraper/src/main.js

31 lines
745 B
JavaScript
Raw Normal View History

2024-04-17 00:39:12 +05:30
import Fastify from 'fastify'
import cors from '@fastify/cors'
2024-04-19 03:22:41 +05:30
import scrapeData from './scraper';
2024-04-16 23:48:32 +05:30
2024-04-17 00:39:12 +05:30
const fastify = Fastify();
2024-04-16 23:48:32 +05:30
2024-04-12 12:24:41 +05:30
// Change this later
const corsOptions = {
2024-04-20 00:52:37 +05:30
origin: 'http://localhost:5173'
2024-04-12 12:24:41 +05:30
}
2024-04-17 00:39:12 +05:30
2024-04-17 00:42:03 +05:30
await fastify.register(cors, corsOptions)
2024-04-12 12:24:41 +05:30
2024-04-18 21:38:41 +05:30
fastify.get('/', async (request, reply) => {
2024-04-20 00:52:37 +05:30
reply.send('Vroom Vroom Vroom');
2024-04-18 21:38:41 +05:30
});
2024-04-19 03:22:41 +05:30
fastify.post('/scrape', async (request, reply) => {
const { url, selectors } = request.body;
try {
2024-04-20 00:52:37 +05:30
const response = await scrapeData(url, selectors);
reply.send(response);
2024-04-19 03:22:41 +05:30
} catch (error) {
2024-04-20 00:52:37 +05:30
reply.status(500).send({ error: error.message });
2024-04-19 03:22:41 +05:30
}
});
2024-04-17 00:40:37 +05:30
await fastify.listen(3000, (err, address) => {
2024-04-12 12:12:55 +05:30
if (err) throw err;
2024-04-12 12:24:41 +05:30
console.log(`Server listening on ${fastify.server.address().port}`)
2024-04-18 22:19:24 +05:30
});