I'm learning NextJS and never create API's. In Next they are created in an easy way but they expose the username and password. I'm saving all the code on Github and I don't want it to be exposed. How can I save them without having to use the .gitignore file?
import { MongoClient } from 'mongodb';
// POST /api/new-meetup
async function handler(req, res) {
if (req.method === 'POST') {
const data = req.body;
const client = await MongoClient.connect(
'mongodb+srv://<nombre-usuario>:<contraseña>@cluster0.gev1u.mongodb.net/meetsup?retryWrites=true&w=majority'
);
const db = client.db();
const meetupsCollection = db.collection('meetups');
const result = await meetupsCollection.insertOne(data);
console.log(result);
client.close();
res.status(201).json({ message: 'Meetup inserted!' });
}
}
export default handler;
you can create an .env file that you should include in your .gitignore
in that file add your environment variables:
then you can use it in your application