Tell us what’s happening:
Describe your issue in detail here.
Your code so far
require(“dotenv”).config();
const express = require(“express”);
const cors = require(“cors”);
const app = express();
const urlparser = require(“url”);
const dns = require(“dns”);
app.use(express.json());
// Basic Configuration
// bodyparser
const bodyparser = require(“body-parser”);
//connecting database
const mongoose = require(“mongoose”);
mongoose.connect(process.env.db, {
useNewUrlParser: true,
useUnifiedTopology: true
});
//making schema
const schema = mongoose.Schema({
url: “String”
});
const Url = mongoose.model(“Url”, schema);
//port
const port = process.env.PORT || 3000;
app.use(cors());
app.use("/public", express.static(${process.cwd()}/public
));
app.get("/", function (req, res) {
res.sendFile(process.cwd() + “/views/index.html”);
});
//using bodyparser
app.use(bodyparser.urlencoded({ extended: false }));
// Your first API endpoint
app.post("/api/shorturl", (req, res) => {
const bodyurl = req.body.url;
console.log(bodyurl);
const something = dns.lookup(
urlparser.parse(bodyurl).hostname,
(error, address) => {
if (!address) {
res.json({ error: “Invalid URL” });
} else {
const url = new Url({ bodyurl });
url.save((err, data) => {
res.json({ original_url:data.url, short_url: data.id });
})
}
console.log(“dns”,error)
console.log(“address”,address)
}
);
console.log(“something”, something);
});
app.get("/api/shorturl/:id", function (req, res) {
const id = req.params.id;
Url.findById(id, (err, data) => {
if (!data) {
res.json({ error: “Invalid URL” });
} else {
res.redirect(data.url);
}
});
});
app.listen(port, function () {
console.log(Listening on port ${port}
);
});
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36
Challenge: Request Header Parser Microservice
Link to the challenge:
https://www.freecodecamp.org/learn/back-end-development-and-apis/back-end-development-and-apis-projects/request-header-parser-microservicestrong text