Skip to content

Commit d56892c

Browse files
committed
adding a example app
1 parent e0746b0 commit d56892c

File tree

6 files changed

+989
-0
lines changed

6 files changed

+989
-0
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
coverage/
22
dist/
33
node_modules/
4+
example/

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,7 @@ ogs({ url: 'https://www.wikipedia.org/', fetchOptions: { headers: { 'user-agent'
133133
console.log('response:', response); // This contains response from the Fetch API
134134
})
135135
```
136+
137+
## Running the example app
138+
139+
Inside the `example` folder contains a simple express app where you can run `npm ci && npm run start` to spin up. Once the app is running, open a web browser and go to `http://localhost:3000/scraper?url=http://ogp.me/` to test it out. There is also a `Dockerfile` if you want to run this example app in a docker container.

example/Dockerfile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# docker build -t open-graph-scraper .
2+
# docker run -dp 127.0.0.1:3000:3000 open-graph-scraper
3+
# http://127.0.0.1:3000/scraper?url=http://ogp.me/
4+
FROM node:20
5+
6+
WORKDIR /usr/src/app
7+
8+
COPY package*.json ./
9+
10+
RUN npm install
11+
12+
COPY . .
13+
14+
EXPOSE 3000
15+
16+
CMD [ "node", "index.js" ]

example/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const express = require('express');
2+
const ogs = require('open-graph-scraper');
3+
const app = express();
4+
const port = 3000;
5+
6+
// http://localhost:3000/scraper?url=http://ogp.me/
7+
app.get('/scraper', async (req, res) => {
8+
if (!req.query.url) return res.send('Missing url query!');
9+
const options = { url: req.query.url };
10+
try {
11+
const data = await ogs(options);
12+
res.send(data);
13+
} catch (error) {
14+
res.send(error);
15+
}
16+
});
17+
18+
app.listen(port, () => {
19+
console.log(`Example open-graph-scraper app listening on port ${port}`);
20+
});

0 commit comments

Comments
 (0)