Hi there,
I’ve been able to finish all of the API/Microservices projects but the Exercise Tracker one. I thought that I had express setup properly like on my other projects, but something isn’t right. When I navigate to the paths from my app.get() or app.post() calls my code should be making a console log and then returning a json object, but it doesn’t do either. Instead, it ends up loading a page with a white background and the phrase ‘not found’ displayed in the upper-right corner.
I’m working from the default glitch app that FCC gives you. I’ve added a .env file for my MongoDB uri and I’ve edited the package.json to use newer versions of express/mongoose/etc. Otherwise, all of my code is in the server.js file, so I’ve copied that below. Let me know if there’s any other information you could use.
Any thoughts you might have about what’s wrong with my app so far would be a huge help. Thanks!
const express = require('express')
const mongo = require('mongodb')
const mongoose = require('mongoose')
const bodyParser = require('body-parser')
const cors = require('cors')
const app = express()
mongoose.connect(process.env.MLAB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true
}).then( () => {
console.log('successfully connected');
},
err => {
console.log('connection error: ', err);
});
app.use(cors())
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json())
app.use(express.static('public'))
app.get('/', (req, res) => {
res.sendFile(__dirname + '/views/index.html')
});
// Not found middleware
app.use((req, res, next) => {
return next({status: 404, message: 'not found'})
})
// Error Handling middleware
app.use((err, req, res, next) => {
let errCode, errMessage
if (err.errors) {
// mongoose validation error
errCode = 400 // bad request
const keys = Object.keys(err.errors)
// report the first validation error
errMessage = err.errors[keys[0]].message
} else {
// generic or custom error
errCode = err.status || 500
errMessage = err.message || 'Internal Server Error'
}
res.status(errCode).type('txt')
.send(errMessage)
})
const listener = app.listen(process.env.PORT || 3000, () => {
console.log('Your app is listening on port ' + listener.address().port)
})
const userSchema = mongoose.Schema({
userName: {type: String, required: true}
});
const user = mongoose.model('user', userSchema);
const exerciseSchema = mongoose.Schema({
userId: {type: String, required: true},
description: {type: String, required: true},
duration: {type: Number, required: true},
date: {type: String, required: false},
});
const exercise = mongoose.model('exercise', exerciseSchema);
app.post('/api/exercise/new-user', (req,res) => {
console.log('test');
res.json({note: 'test'});
});
app.get('/test', (req,res) => {
console.log('test');
res.json({note: 'test'});
});