How to Set Up NodeJS and MongoDB on GitHub Codespaces

How to Set Up NodeJS and MongoDB on GitHub Codespaces

Setting up NodeJS and MongoDB on GitHub Codespaces is a straightforward process that can be completed in a few steps. Firstly, create a new repository on GitHub and open it in Codespaces. Then, select the environment you want to use for your project.

Next, install NodeJS and MongoDB by running the following commands in the terminal:

sqlCopy codesudo apt-get update
sudo apt-get install nodejs
sudo apt-get install mongodb

After installing NodeJS and MongoDB, start the MongoDB server by running:

sqlCopy codesudo systemctl start mongod

You can now create a new NodeJS project by running:

csharpCopy codenpm init

Install the required packages and dependencies using the following command:

Copy codenpm install express mongodb

Create a new JavaScript file to write your server-side code, and connect to the MongoDB database using the following code:

javascriptCopy codeconst MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017/myproject';

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  console.log("Database created!");
  db.close();
});

Finally, start the NodeJS server by running:

Copy codenode server.js

These are the basic steps to set up NodeJS and MongoDB on GitHub Codespaces. You can customize and configure the settings and packages based on your project requirements.