This README will guide you through the process of setting up and testing the application on your local server using Node.js and MongoDB.
Before you begin, ensure you have the following installed on your local machine:
- Node.js (v20 or higher) - Install Node.js
- MongoDB (installed locally or use a cloud-based solution like MongoDB Atlas) - Install MongoDB
- Postman (Optional, for API testing) - Download Postman
If you haven't already, clone the repository from GitHub:
git clone https://github.com/amowogbaje/node-search-api.git
cd node-search-api
After cloning the repository, navigate to the project directory and install the required dependencies:
npm install
To securely store your application configurations like the database URI, you need to set up a .env
file.
Create a file named .env
in the root directory of your project and add the following:
MONGODB_URI=mongodb://localhost:27017/your_database_name
PORT=3000
Replace your_database_name
with the name you want for your local MongoDB database.
If you’re using MongoDB locally:
- Ensure MongoDB is installed and running.
- To start MongoDB, open a new terminal window and run the following command:
mongod
This will start the MongoDB server on the default port 27017
.
If you are using MongoDB Atlas (cloud database), ensure your connection string in .env
is set accordingly.
Once everything is set up, you can start your application by running the following command:
npm run dev
This will start the Node.js server on the specified port (default is 3000
). You should see output similar to this:
Server is running on http://localhost:3000
You can now test your application on your local server.
GET Request (Example):
- Endpoint:
GET http://localhost:3000/api/search?query=food&page=1&limit=10
In Postman:
- Open Postman.
- Set the HTTP method to
GET
. - Enter the URL
http://localhost:3000/api/search?query=food&page=1&limit=10
. - Click "Send" to view the response.
To stop the application, press Ctrl + C
in the terminal where the server is running.
Now you have a local server running with Node.js and MongoDB! You can continue developing, testing, and making changes locally before deploying to production (Heroku or another platform). If you encounter any issues, feel free to reach out for assistance.
Happy coding! 🎉