const express = require('express');
const app = express();
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
app.use(bodyParser.json());
const PORT = process.env.PORT || 3000;
try {
mongoose.connect('mongodb://localhost/YOUR_DB_NAME', {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
}, () =>
console.log("connected"));
} catch (error) {
console.log("could not connect");
}
const Schema = mongoose.Schema
const BookSchema = new Schema({
name : String
})
const BookSchema = mongoose.Schema({
title: {
type: String,
required: true,
},
price: {
type: Number,
},
});
const Model = mongoose.model;
const Book = Model("Books", BookSchema);
app.get("/books", async (req, res) => {
try {
const book = await Book.find({});
res.json({ success: true, book });
} catch (error) {
res.json({ success: false, msg: "failed to fetch book" });
}
});
app.get('/', (req, res) => {
res.send('check your browser console for any error , if no error you are good!');
});
app.listen(PORT, () => console.log(`Server is listening on port ${PORT}`));