Skip to content

Instantly share code, notes, and snippets.

@hdd42

hdd42/server.js

Created Mar 28, 2018
Embed
What would you like to do?
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const cors = require('cors');
const {crawl} = require('./crawler')
//web sunucusunu olustur.
const app = express();
//web sunucusu icin genel ayarlar.
app.set('json spaces', 2)
app.use(cors())
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
//kullaniciya web form u goster
app.get('/', (req,res,next) =>{
res.status(200).sendFile(__dirname+'/index.html')
})
//crawl bilgilerini all
app.use('/crawl', async (req,res,next) =>{
//form dan gelen bilgileri al.
let {url , cssSelector , output} = req.body || req.query || null
//eger gerekli alanlar gelmediyse, hata dondur.
if (!url || !cssSelector || !output ){
return res.status(400).send({error :'url, css seciciler ve cikti alani zorunludur'})
}
//crawl islemini baslat
else{
console.log(`url : ${url} , cssSelector : ${cssSelector} , output:${output}`)
let results = await crawl({url,cssSelector,output}).catch(err =>{
console.log("Bir hata olsutu : ");
return res.send({error:err.message || err})
})
//bir hata yok, crawl islemi basarili
res.status(200).send({results})
}
})
//Un expected error handler
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.json({
'error': {
message: err.message || err,
}
});
});
app.listen(3000,()=>console.log('Uygulama , http://localhost:3000 de calisiyor'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment