I'm trying to use the Aeries API to be able to get grade and school information. I want the user to be able to enter a code into it and it will go into the url like http://localhost:3000/aeries/school/0 which will send back the JSON data for that school, but instead it looks like http://localhost:3000/aeries/school/?code=0 . How do I get this to send the data, instead of sending the 404 error: Cannot GET /aeries/school/ . I'm kind of lost, here's my code:JS:var express = require('express'); var mongoose = require('mongoose'); var ejs = require('ejs'); var bodyParser = require('body-parser'); var req = require('request'); var rp = require('request-promise'); var app = require('express')(); var server = require('http').Server(app); var io = require('socket.io')(server); const request = require('request'); const fetch = require('node-fetch'); fetch.Promise = rp; var resultTask=[]; var db = mongoose.connection; var cursor = db.collection('tasks').find(); //Using modules(idk what this means really) app.use(express.static('./')); app.set('view engine', 'ejs'); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); //Todo page app.get('/todo', (req,res,next)=>{ mongoose.connect('mongodb://localhost/todo', {useNewUrlParser:true}, function(){ resultTask=[]; var cursor = db.collection('tasks').find(); cursor.forEach(function(task){ resultTask.push(task); }, function() { db.close(); res.render('todoPage', {tasks: resultTask}); }) }); }); //Home page app.get('/', (req,res)=>{ res.render('landing') }); const uriBase = 'https://demo.aeries.net/aeries/api/v3/'; var options = { method: "GET", uri: uriBase, headers: { 'Accept': 'application/json, text/html, application/xhtml+xml, */*', 'AERIES-CERT' : '477abe9e7d27439681d62f4e0de1f5e1' }, json: true // Automatically parses the JSON string in the response }; app.get('/aeries/school/:code', async (req,res)=>{ const { code } = req.params; if(!code) return res.end('No code provided'); options.uri = `${uriBase}/schools/${code}`; let data; try { data = await rp(options); res.send(data) } catch(err) { res.send({err, msg: 'No school found'}); } if(data) return res.send(data[0]); res.code(404).send('No school found'); }) //Error handling for server db.on('error', console.error.bind(console, 'MongoDB connection error:')); //Handling adding new task to db app.post('/addTask', (req,res ,next) =>{ var checkbox = req.body['i']; var taskSchema = { task: req.body.newTask, checked: false }; mongoose.connect('mongodb://localhost/todo', {useNewUrlParser:true}, function(){ db.collection('tasks').insertOne(taskSchema, function(err, res){ console.log('inserted.') db.close(); }) if(checkbox){ console.log('the box WAS checked'); } else{ console.log('the box was NOT checked'); } }); //Redirect back to landing after adding new task res.redirect('/'); }); //Starts server server.listen(3000, () => { console.log('Listening on port 3000'); }); EJS:<% include partials/header%>
Submitted March 16, 2019 at 07:07PM by LawlessWalrus1
No comments:
Post a Comment