Search & show listings
This commit is contained in:
62
backend/server.js
Normal file
62
backend/server.js
Normal file
@@ -0,0 +1,62 @@
|
||||
import express from 'express'
|
||||
import bodyParser from 'body-parser';
|
||||
var MongoClient = require('mongodb').MongoClient;
|
||||
var url = 'mongodb://localhost:27017/example';
|
||||
|
||||
require("babel-polyfill");
|
||||
|
||||
const router = express.Router({mergeParams: true})
|
||||
|
||||
const PORT = process.env.PORT || 3001;
|
||||
const AGENTURA_KEY = process.env.AGENTURA_KEY || '1somethingverysecret';
|
||||
|
||||
// TODO:
|
||||
// db.results.ensureIndex({loc:"2d"})
|
||||
//collection.ensureIndex("username",callback)
|
||||
|
||||
router.get('/search', async (req, res, next) => {
|
||||
try {
|
||||
const bounds = req.query.bounds || '';
|
||||
const db = await MongoClient.connect(url);
|
||||
const properties = db.collection('results');
|
||||
let query = {};
|
||||
|
||||
if (bounds) {
|
||||
const [lat1, lng1, lat2, lng2] = bounds.split(',').map(parseFloat)
|
||||
const box = [[lat1, lng1], [lat2, lng2]];
|
||||
|
||||
query = Object.assign(query, {
|
||||
loc: {
|
||||
"$geoWithin": {
|
||||
"$box": box
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const all = await properties.find(query).toArray();
|
||||
|
||||
res.json(all);
|
||||
res.end();
|
||||
await db.close();
|
||||
} catch (e) {
|
||||
console.log('error:', e);
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
const app = express()
|
||||
app.use(bodyParser.json());
|
||||
|
||||
app.use(function(req, res, next) {
|
||||
res.header("Access-Control-Allow-Origin", "*");
|
||||
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
||||
res.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
|
||||
res.header('Access-Control-Allow-Credentials', 'true');
|
||||
next();
|
||||
});
|
||||
|
||||
app.use('/api', router);
|
||||
app.listen(PORT, () => console.log('Express server running at localhost: ' + PORT));
|
||||
|
||||
Reference in New Issue
Block a user