After I enter the URL to complete the challenge, it displays this:
// running tests
"mongodb" dependency should be in package.json
"mongoose" dependency should be in package.json
"mongoose" should be connected to a database
// tests completed
After trying to start from scratch with fresh files, and even copying and pasting the solution, the same error keeps occuring. Am I doing something wrong?
package.json
{
"name": "fcc-mongo-mongoose-challenges",
"version": "0.0.1",
"description": "A boilerplate project",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.17.1",
"body-parser": "^1.19.0",
"mongoose": "^5.1.6",
"mongodb": "^3.0.10",
"fcc-express-bground": "git+https://github.com/Em-Ant/fcc-express-bground-pkg.git"
},
"engines": {
"node": "4.4.5"
},
"repository": {
"type": "git",
"url": "https://hyperdev.com/#!/project/welcome-project"
},
"keywords": [
"node",
"hyperdev",
"express"
],
"license": "MIT"
}
myApp.js
var express = require('express');
var app = express();
var bodyParser = require("body-parser");
const mongoose = require("mongoose");
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });
// --> 7) Mount the Logger middleware here
app.use((req, res, next) => {
console.log(`${req.method} ${req.path} - ${req.ip}`);
next();
})
// --> 11) Mount the body-parser middleware here
// used to process x-www-form-urlencoded into req.body
// Only works via postman with extended: true?
app.use(express.urlencoded({extended: false}));
// used to process json into req.body
//app.use(bodyParser.json());
/** 1) Meet the node console. */
//console.log("Hello World");
/** 2) A first working Express Server */
/*
app.get("/", function(req, res) {
res.send("Hello Express");
});
*/
/** 3) Serve an HTML file */
app.get("/", (req, res) => {
res.sendFile(__dirname + "/views/index.html");
});
/** 4) Serve static assets */
app.use(express.static(__dirname + "/public"));
/** 5) serve JSON on a specific route */
app.get("/json", (req, res) => {
let string = "Hello json";
if (process.env.MESSAGE_STYLE == "uppercase") {
string = string.toUpperCase()
}
res.json({"message": string});
});
/** 6) Use the .env file to configure the app */
//console.log(process.env.MESSAGE_STYL == undefined);
/** 7) Root-level Middleware - A logger */
// place it before all the routes !
/** 8) Chaining middleware. A Time server */
const middleware = (req, res, next) => {
req.time = new Date().toString();
next();
};
app.get("/now", middleware, (req, res) => {
res.json({"time": req.time});
});
/** 9) Get input from client - Route parameters */
app.get("/:word/echo", (req, res) => {
let word = req.params.word;
console.log(word);
res.json({"echo": word});
});
/** 10) Get input from client - Query parameters */
// /name?first=<firstname>&last=<lastname>
app.get("/name", (req, res) => {
res.json({"name": `${req.query.first} ${req.query.last}`});
});
// can use
// "app.route(path).get(handler).post(handler)"
// to handle multiple methods for each path
/** 11) Get ready for POST Requests - the `body-parser` */
// place it before all the routes !
// General POST,
/*
app.post("/name", (req, res) => {
console.log(JSON.stringify(req.body));
res.json(req.body);
})
*/
/** 12) Get data form POST */
// Only works with first & last
app.post("/name", (req, res) => {
res.json({"name": `${req.body.first} ${req.body.last}`});
});
// This would be part of the basic setup of an Express app
// but to allow FCC to run tests, the server is already active
/** app.listen(process.env.PORT || 3000 ); */
//---------- DO NOT EDIT BELOW THIS LINE --------------------
module.exports = app;
.env (username and password have placeholders)
MONGO_URI="mongodb+srv://<username>:<password>@cluster0-jyzfd.mongodb.net/test?retryWrites=true&w=majority"
MESSAGE_STYLE=uppercase