MAJOR PROJECT (BASICS AND LISTING MODEL)
APP.JS
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const MONGO_URL = "mongodb://127.0.0.1:27017/wanderlust";
const Listing = require("./models/listing.js");
async function main() {
await mongoose.connect(MONGO_URL);
}
main()
.then(() => {
console.log("connected to DB");
})
.catch((err) => {
console.log(err);
});
app.get("/", (req, res) => {
res.send("home");
});
app.get("/testListing", async(req, res) => {
let sampletesting = new Listing({
title: "villa",
description: "By the Beach",
price: 3000,
location: "goa",
country: "india",
});
await sampletesting.save();
console.log("sample saved");
res.send("successful");
});
app.listen(8080, () => {
console.log("listening to port:8080");
});
LISTING.JS
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
listingSchema = new Schema({
title: {
type: String,
required: true,
},
description: String,
image: {
type: String,
default:
"https://plus.unsplash.com/premium_photo-1732568817442-342a8c77fb80?w=600&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxmZWF0dXJlZC1waG90b3MtZmVlZHwyOXx8fGVufDB8fHx8fA%3D%3D",
set: (v) =>
v === ""
? "https://plus.unsplash.com/premium_photo-1732568817442-342a8c77fb80?w=600&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxmZWF0dXJlZC1waG90b3MtZmVlZHwyOXx8fGVufDB8fHx8fA%3D%3D"
: v,
},
price: Number,
location: String,
country: String,
});
const Listing = mongoose.model("Listing", listingSchema);
module.exports = Listing;

Comments
Post a Comment