mirror of
https://github.com/intergalacticalvariable/reader.git
synced 2024-11-15 19:22:20 +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 { 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,33 +24,14 @@ 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, () => {
|
||||
console.log(`Server is running on port ${port}`);
|
||||
});
|
||||
|
||||
export default app;
|
||||
export default app;
|
Loading…
Reference in New Issue
Block a user