Answers for "express mongodb"

2

express mongodb

//IMPORTS
import express from "express";
import mongoose from "mongoose";

const app = express();

//MIDDLEWARES
app.use("/");

//MIDDLEWARE TO TRANSFER DATA
app.use(express.urlencoded({ extended: true }));
app.use(express.json({ limit: "30mb", extended: true }));

//CONSTANTS
const CONNECTION_PORT = "YOUR_MONGODB_CONNECTION_LINK_HERE";
const PORT = process.env.PORT || 5000;

//CONNECTION WITH MONGOOSE DB AND LISTENING TO PORT
mongoose
  .connect(CONNECTION_PORT, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  })
  .then(() => {
    app.listen(PORT, () => {
      console.log(`Server Running on port ${PORT}`);
    });
  })
  .catch((err) => {
    console.log(err);
  });
Posted by: Guest on August-31-2021
1

how to access a database in express

var MongoClient = require('mongodb').MongoClient

MongoClient.connect('mongodb://localhost:27017/animals', function (err, client) {
  if (err) throw err

  var db = client.db('animals')

  db.collection('mammals').find().toArray(function (err, result) {
    if (err) throw err

    console.log(result)
  })
})
Posted by: Guest on November-21-2019
0

how to access a database in express

var mysql = require('mysql')
var connection = mysql.createConnection({
  host: 'localhost',
  user: 'dbuser',
  password: 's3kreee7',
  database: 'my_db'
})

connection.connect()

connection.query('SELECT 1 + 1 AS solution', function (err, rows, fields) {
  if (err) throw err

  console.log('The solution is: ', rows[0].solution)
})

connection.end()
Posted by: Guest on November-21-2019
0

expressjs with mongodb

app.post('/quotes', (req, res) => { console.log(req.body) })You should be able to get an object similar to the following in your command line:{name: 'pdp',quotes: 'pdp psr pdp'}
Posted by: Guest on April-13-2021

Code answers related to "express mongodb"

Code answers related to "Javascript"

Browse Popular Code Answers by Language