add an express endpoint to run the crawl endpoint

This commit is contained in:
Harsh Gupta 2024-08-14 19:34:53 +05:30
parent 57b07507d1
commit 3e2bf6d39d

View File

@ -1,3 +1,4 @@
import "reflect-metadata"
import express from 'express';
import { container } from 'tsyringe';
import { CrawlerHost } from './cloud-functions/crawler';
@ -5,12 +6,17 @@ import { CrawlerHost } from './cloud-functions/crawler';
const app = express();
const port = process.env.PORT || 3000;
container.registerSingleton(CrawlerHost);
const crawlerHost = container.resolve(CrawlerHost);
app.use(express.json());
app.post('/crawl', async (req, res) => {
// Example curl for /crawl:
// curl -X GET "http://localhost:3000/https://example.com"
app.get('/:url(*)', async (req, res) => {
try {
const url = req.params.url;
await crawlerHost.crawl(req, res);
} catch (error) {
console.error('Error during crawl:', error);
@ -18,29 +24,10 @@ app.post('/crawl', async (req, res) => {
}
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
export default app;
import express from 'express';
import { container } from 'tsyringe';
import { CrawlerHost } from './cloud-functions/crawler';
const app = express();
const port = process.env.PORT || 3000;
const crawlerHost = container.resolve(CrawlerHost);
app.use(express.json());
app.post('/crawl', async (req, res) => {
try {
await crawlerHost.crawl(req, res);
} catch (error) {
console.error('Error during crawl:', error);
res.status(500).json({ error: 'An error occurred during the crawl' });
}
// Example curl for /hello:
// curl -X GET "http://localhost:3000/hello"
app.get('/hello', (req, res) => {
res.json({ message: 'Hello, World!' });
});
app.listen(port, () => {