initial commit (app returns google/startpage results)
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
const express = require('express');
|
||||
const path = require('path');
|
||||
const handleSearch = require('./search');
|
||||
|
||||
const app = express();
|
||||
const port = 3000;
|
||||
|
||||
app.get('/', (req, res) => {
|
||||
res.sendFile(path.join(__dirname, '/static/index.html'));
|
||||
});
|
||||
|
||||
app.get('/search', handleSearch);
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`App listening on port ${port}`);
|
||||
});
|
||||
@@ -0,0 +1,45 @@
|
||||
const cheerio = require("cheerio");
|
||||
|
||||
async function handleSearch(req, res) {
|
||||
try {
|
||||
const query = req.query.query;
|
||||
|
||||
if (!query) {
|
||||
return res.status(400).send("Missing search query");
|
||||
}
|
||||
|
||||
/* here we use a searx.ng instance (a meta-search engine) and filter out google search results.
|
||||
* by testing, i've found out that !goc (google's programmable search engine) is more reliable for
|
||||
* this kind of operation.
|
||||
* the instance could be changed if the current one e.g. goes out of service.
|
||||
*/
|
||||
/**
|
||||
* more instances:
|
||||
* https://baresearch.org/
|
||||
*/
|
||||
//const url = `https://baresearch.org/search?q=!goc+${encodeURIComponent(query)}`;
|
||||
const searxngUrl = process.env.SEARXNG_URL || "http://localhost:8080";
|
||||
const params = new URLSearchParams({
|
||||
q: query,
|
||||
engines: "startpage",
|
||||
format: "json"
|
||||
});
|
||||
|
||||
const response = await fetch(
|
||||
`${searxngUrl}/search?${params}`,
|
||||
{
|
||||
headers: {
|
||||
Accept: "application/json"
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
const data = await response.json();
|
||||
res.json(data.results || []);
|
||||
} catch (error) {
|
||||
console.error("[SEARCH.JS] " + error);
|
||||
res.status(500).send("Search failed");
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = handleSearch;
|
||||
@@ -0,0 +1,12 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Searchthing</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>search the web</h1>
|
||||
<form action="/search" name="searchbox" method="GET">
|
||||
search: <input type="text" name="query"/>
|
||||
<input type="submit" value="submit"/>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user