Skip to content

Commit 39eded8

Browse files
committed
fix: config port and host
1 parent 8f0fbc5 commit 39eded8

File tree

5 files changed

+27
-15
lines changed

5 files changed

+27
-15
lines changed

.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
API_PORT=5000
2+
API_HOST=localhost
3+
NODE_ENV=development

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ crawlee_storage
88
storage
99

1010
# any output from the crawler
11-
*.json
11+
*.json
12+
.env

package-lock.json

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"commander": "^11.1.0",
1111
"cors": "^2.8.5",
1212
"crawlee": "^3.0.0",
13+
"dotenv": "^16.3.1",
1314
"express": "^4.18.2",
1415
"express-fileupload": "^1.4.3",
1516
"glob": "^10.3.10",
@@ -34,6 +35,7 @@
3435
"start:dev": "NODE_ENV=development npm run build && node dist/src/main.js",
3536
"start:server": "NODE_ENV=development npm run build && node dist/src/server.js",
3637
"start:prod": "node dist/main.js",
38+
"start:server:prod": "npm run build && node dist/src/server.js",
3739
"build": "tsc",
3840
"fmt": "prettier --write ."
3941
},

src/server.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
1-
// file: app/src/api.ts
2-
31
import express from 'express';
42
import cors from 'cors';
5-
import { readFile, writeFile } from 'fs/promises';
3+
import { readFile } from 'fs/promises';
64
import { crawl, write } from "./core.js";
75
import { Config } from './config.js';
6+
import { configDotenv } from 'dotenv';
7+
8+
configDotenv();
89

9-
// Create a new express application instance
1010
const app = express();
11-
const port = 3000; // You may want to make the port configurable
11+
const port = Number(process.env.API_PORT) || 3000;
12+
const hostname = process.env.API_HOST || 'localhost';
1213

13-
// Enable JSON and file upload functionality
1414
app.use(cors());
1515
app.use(express.json());
1616

1717
// Define a POST route to accept config and run the crawler
1818
app.post('/crawl', async (req, res) => {
19-
// Read the configuration file sent as form-data
2019
const config: Config = req.body;
21-
22-
// Placeholder for handling crawler events and operations
2320
try {
2421
await crawl(config);
2522
await write(config);
26-
27-
// Read the output file after crawling and send it in the response
2823
const outputFileContent = await readFile(config.outputFileName, 'utf-8');
2924
res.contentType('application/json');
3025
return res.send(outputFileContent);
@@ -33,9 +28,8 @@ app.post('/crawl', async (req, res) => {
3328
}
3429
});
3530

36-
// Start the Express server
37-
app.listen(port, () => {
38-
console.log(`API server listening at http://localhost:${port}`);
31+
app.listen(port, hostname, () => {
32+
console.log(`API server listening at http://${hostname}:${port}`);
3933
});
4034

4135
export default app;

0 commit comments

Comments
 (0)