Hello to all people,
I was working on the Use body-parser to Parse POST Requests, and I got stumped, so I looked at the forum to see if anybody had the problem I was having. I saw that there was a guide about it, so I thought, “Why not try?” After that, I tried doing what it said, and then I realized my mistake, which was that I deleted the "fcc-express-bground": "https://github.com/freeCodeCamp/fcc-express-bground-pkg.git
line, so I brought it back, and now it won’t let me run the script.
What do I do?
myApp.js:
// Load environment variables at the very beginning of your app
require("dotenv").config();
const { json } = require("body-parser");
const express = require("express");
const req = require("express/lib/request");
const app = express();
const path = require("path");
// Step 7 (HAS TO BE FIRST IN ORDER TO WORK)
app.use((req, res, next) => {
console.log(`${req.method} ${req.path} - ${req.ip}`);
next();
})
// Step 1
app.get("/", function (req, res) {
res.send("Hello json");
});
// Step 2
app.get("/another-route", function (req, res) {
let filePath = path.join(__dirname, "/views/index.html");
res.sendFile(filePath);
});
// Step 3
let publicPath = path.join(__dirname, "/public");
app.use("/public", express.static(publicPath));
// Step 4
let helloObj = { message: "Hello json" };
app.get("/json-default", function (req, res) {
res.json(helloObj);
});
// Step 5 + 6
app.get("/json", function(req, res){
if(process.env.MESSAGE_STYLE==="uppercase"){
res.json({"message":"HELLO JSON"})
}else{
res.json({"message":"Hello json"})
}
})
// Step 8
app.get(
"/now",
(req, res, next) => {
req.time = new Date().toString();
next();
},
(req, res) => {
res.send({
time: req.time
});
}
);
// Step 9
app.get("/:word/echo", (req, res) => {
const { word } = req.params;
res.json({
echo: word
});
});
// Step 10
app.get("/name", function(req, res) {
var firstName = req.query.first;
var lastName = req.query.last;
var { first: firstName, last: lastName } = req.query;
res.json({
name: `${firstName} ${lastName}`
});
});
// Step 11
app.post('/name', (req, res) => {
const firstname = req.body.first;
const lastname = req.body.last;
console.log(firstname + " " + lastname)
const names = firstname + " " + lastname
res.json({name: names })
})
module.exports = app;
package.json:
{
"name": "fcc-learn-node-with-express",
"version": "0.1.0",
"dependencies": {
"body-parser": "^1.15.2",
"cookie-parser": "^1.4.3",
"dotenv": "^16.0.1",
"express": "^4.14.0",
"fcc-express-bground": "https://github.com/freeCodeCamp/fcc-express-bground-pkg.git"
},
"main": "server.js",
"scripts": {
"start": "node server.js"
}
}
Error:
node:internal/modules/cjs/loader:1252
throw err;
^
Error: Cannot find module 'fcc-express-bground'
Require stack:
- /workspace/boilerplate-express/server.js
at Function._resolveFilename (node:internal/modules/cjs/loader:1249:15)
at Function._load (node:internal/modules/cjs/loader:1075:27)
at TracingChannel.traceSync (node:diagnostics_channel:315:14)
at wrapModuleLoad (node:internal/modules/cjs/loader:218:24)
at Module.require (node:internal/modules/cjs/loader:1340:12)
at require (node:internal/modules/helpers:141:16)
at Object.<anonymous> (/workspace/boilerplate-express/server.js:6:17)
at Module._compile (node:internal/modules/cjs/loader:1546:14)
at Object..js (node:internal/modules/cjs/loader:1689:10)
at Module.load (node:internal/modules/cjs/loader:1318:32) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/workspace/boilerplate-express/server.js' ]
}
Please tell me what I’m doing wrong simply and as soon or as late as you want.