// for parsing application/x-www-form-urlencoded app.route('/') .get((req, res) => { if (req.query.code) { fbMe.getToken(req.query.code) res.redirect('/?status=ok') } else if (req.query.status) { res.send(req.query.status) } else { const loginURL = fbMe.getLoginUrl() res.send('<a href="' + loginURL + '">Get access from Facebook</a>') } })
// or app.route('/products') .get(function (req, res, next) {}) .post(function (req, res, next) {}) .put(function (req, res, next) {})
app.route('/login') .get(function (req, res) { res.send('this is the login form'); }) .post(function (req, res) { console.log('processing'); res.send('processing the login form!'); });
app.route('/delete/:id') .get((req, res) => { var id = req.params.id; db.collection('data').deleteOne({_id: ObjectId(id)}, (err, result) => { if (err) return res.send(500, err) console.log('Deletado do Banco de Dados!'); res.redirect('/show'); }) })
app.route('/', (req, res) => { res.sendStatus(200); });
/** * Serve the basic index.html with upload form */ app.route('/').get((req, res) => { res.writeHead(200, {'Content-Type': 'text/html'}); res.write('<form action="upload" method="post" enctype="multipart/form-data">'); res.write('<input type="file" name="fileToUpload"><br>'); res.write('<input type="submit">'); res.write('</form>'); return res.end(); });
function () { function App() { this.app = express(); this.middleware(); this.database = new db_1.default(); this.database.createConnection(); this.controller = new controller_1.default(); this.routes(); } App.prototype.middleware = function () { this.app.use(bodyparser.json()); this.app.use(bodyparser.urlencoded({ extended: true })); }; App.prototype.routes = function () { var _this = this; this.app.route('/').get(function (req, res) { return res.status(200).json({ "result": "Hello World" }); }); this.app.route('/api/crushs').get(function (req, res) { return _this.controller.select(req, res); }); this.app.route('/api/crushs/:id').get(function (req, res) { return _this.controller.selectOne(req, res); }); this.app.route('/api/crushs/:id').delete(function (req, res) { return _this.controller.delete(req, res); }); this.app.route('/api/crushs/:id').put(function (req, res) { return _this.controller.update(req, res); }); this.app.route('/api/crushs').post(function (req, res) { return _this.controller.insert(req, res); }); }; return App; }()
/* ========================================================== Create a Route (/upload) to handle the Form submission (handle POST requests to /upload) Express v4 Route definition ============================================================ */ app.route('/upload') .post(function (req, res, next) { var fstream; req.pipe(req.busboy); req.busboy.on('file', function (fieldname, file, filename) { console.log("Uploading: " + filename); //Path where image will be uploaded fstream = fs.createWriteStream(__dirname + '/files/' + filename); file.pipe(fstream); fstream.on('close', function () { console.log("Upload Finished of " + filename); res.redirect('/demo/uploadfile?fn=' + filename); //where to go next }); }); });
app.route('/edit/:id') .get((req, res) => { var id = req.params.id; db.collection('data').find(ObjectId(id)).toArray((err, result) => { if (err) return res.send(err); res.render('edit.ejs', {data: result}) }) }) .post((req, res) => { var id = req.params.id; var name = req.body.name; var surname = req.body.surname; db.collection('data').updateOne({_id: ObjectId(id)}, { $set: { name: name, surname: surname } }, (err, result) => { if (err) return res.send(err); res.redirect('/show') console.log('Atualizado no Banco de Dados!'); }) });
/** * Query the site navigation with every route */ app.route('*').get((req, res, next) => { req.prismic.api.getSingle('navigation').then(function(navContent){ // Define the navigation content res.locals.navContent = navContent; next(); }); });
// GET /edu/questions edu.route('/questions').get(async (req, res) => { try { const questions = await QuestionCtrl.list(req.query || {}) const isActive = isActivePage(req) // question._id --> URL const imagePaths = questions.reduce((map, question) => { map[question._id] = frontEndPath( question.imageSrc, edu.locals.frontEndRoot ) return map }, {}) res.render('edu/questions/index', { questions, isActive, imagePaths }) } catch (error) { res.status(500).send(`<h1>Internal Server Error</h1> <pre>${error}</pre>`) } })
// GET /edu/questions/new edu.route('/questions/new').get((req, res) => { const question = { possibleAnswers: [{ val: 'a' }, { val: 'b' }, { val: 'c' }, { val: 'd' }] } const isActive = isActivePage(req) res.render('edu/questions/new', { question, isActive }) })
// Make sure that he upload path exits /** * Create route /upload which handles the post request */ app.route('/upload').post((req, res, next) => { req.pipe(req.busboy); // Pipe it trough busboy req.busboy.on('file', (fieldname, file, filename) => { console.log(`Upload of '${filename}' started`); // Create a write stream of the new file const fstream = fs.createWriteStream(path.join(uploadPath, filename)); // Pipe it trough file.pipe(fstream); // On finish of the upload fstream.on('close', () => { console.log(`Upload of '${filename}' finished`); res.redirect('back'); }); }); });
// RSS API app.route('/api/rss') .get(function(req, res) { feed.load("https://lenta.ru/rss/news", function(err, articles) { res.json(articles); }); });
app.route('/schema').get((req, res) => { res.send(routes); });