Skip to content

Commit

Permalink
updated code on 11 sep
Browse files Browse the repository at this point in the history
  • Loading branch information
ReactWithRajesh authored Sep 11, 2023
1 parent 1f401fc commit 72d9d5a
Show file tree
Hide file tree
Showing 1,324 changed files with 344,181 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
MONGO_URI: "mongodb+srv://rjReactDev:Deadrjs%[email protected]/?retryWrites=true&w=majority";
PORT:4040
45 changes: 42 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,50 @@
const port = 4040

const express = require('express')
const app = express()
const morgan = require('morgan')
const env = require('dotenv')
const mongoose=require('mongoose')
const { MongoClient, ServerApiVersion } = require('mongodb');
env.config()
const port = process.env.PORT || 4040
const uri ="mongodb+srv://rjReactDev:[email protected]/?retryWrites=true&w=majority";

//db connection
// mongoose.connect(uri)
// .then(()=>console.log('Db connected.'))
// mongoose.connection.on('error',err=>{
// console.log(`db connection has some error: ${err.message}`)
// })

const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
}
});
async function run() {
try {
// Connect the client to the server (optional starting in v4.7)
await client.connect();
// Send a ping to confirm a successful connection
// await client.db("admin").command({ ping: 1 });
console.log("Pinged your deployment. You successfully connected to MongoDB!");
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}
}
run().catch(console.dir);

//const postRoutes =require('./routes/post')

//using destructuring method
const { getPosts } = require('./routes/post')
//const { getPosts } = require('./routes/post')

//app.get("/", getPosts)
//using controller
const postRouter = require('./routes/post')

// const myOwnMiddleWare = (req, res, next) => {
// console.log("my own ")
Expand All @@ -17,7 +55,8 @@ const { getPosts } = require('./routes/post')

app.use(morgan('dev'))

app.get("/", getPosts)
app.use("/", postRouter)

app.listen(port, () => [
console.log(`Server listening on port : ${port}`)
])
8 changes: 8 additions & 0 deletions controllers/post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
exports.getPosts = (req, res) => {
// res.send('<h1>Welcome to Node js Server</h1>')

//need to json return as response
res.json({
posts: [{ title: 'ReactJs' }, { title: 'Redux' }, { title: 'Javascript' }, { title: 'node' }]
})
}
Loading

0 comments on commit 72d9d5a

Please sign in to comment.