mirror of
https://github.com/intergalacticalvariable/reader.git
synced 2024-11-16 11:42:32 +08:00
add an express endpoint to run the crawl endpoint
This commit is contained in:
parent
57b07507d1
commit
3e2bf6d39d
|
@ -1,3 +1,4 @@
|
||||||
|
import "reflect-metadata"
|
||||||
import express from 'express';
|
import express from 'express';
|
||||||
import { container } from 'tsyringe';
|
import { container } from 'tsyringe';
|
||||||
import { CrawlerHost } from './cloud-functions/crawler';
|
import { CrawlerHost } from './cloud-functions/crawler';
|
||||||
|
@ -5,12 +6,17 @@ import { CrawlerHost } from './cloud-functions/crawler';
|
||||||
const app = express();
|
const app = express();
|
||||||
const port = process.env.PORT || 3000;
|
const port = process.env.PORT || 3000;
|
||||||
|
|
||||||
|
container.registerSingleton(CrawlerHost);
|
||||||
|
|
||||||
const crawlerHost = container.resolve(CrawlerHost);
|
const crawlerHost = container.resolve(CrawlerHost);
|
||||||
|
|
||||||
app.use(express.json());
|
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 {
|
try {
|
||||||
|
const url = req.params.url;
|
||||||
await crawlerHost.crawl(req, res);
|
await crawlerHost.crawl(req, res);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error during crawl:', error);
|
console.error('Error during crawl:', error);
|
||||||
|
@ -18,29 +24,10 @@ app.post('/crawl', async (req, res) => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
app.listen(port, () => {
|
// Example curl for /hello:
|
||||||
console.log(`Server is running on port ${port}`);
|
// curl -X GET "http://localhost:3000/hello"
|
||||||
});
|
app.get('/hello', (req, res) => {
|
||||||
|
res.json({ message: 'Hello, World!' });
|
||||||
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' });
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user