Profile API routes module.
- Source:
Examples
// GET api/profile/me
// Usage:
// 1. Include auth middleware in your express app
const auth = require('../../middleware/auth');
// 2. Add the following route in your express app
app.get('/api/profile/me', auth, async (req, res) => {
// Your route handler logic
});
// 3. Send a GET request with the token in the 'x-auth-token' header
// e.g., using axios:
const axios = require('axios');
axios.get('http://localhost:5000/api/profile/me', {
headers: {
'x-auth-token': 'your_token_here'
}
}).then(response => {
console.log(response.data); // User's profile
}).catch(error => {
console.error('Error:', error);
});
// POST api/profile
// Usage:
// 1. Include auth middleware in your express app
const auth = require('../../middleware/auth');
// 2. Add the following route in your express app
app.post('/api/profile', auth, async (req, res) => {
// Your route handler logic
});
// 3. Send a POST request with the token in the 'x-auth-token' header
// e.g., using axios:
const axios = require('axios');
axios.post('http://localhost:5000/api/profile', {
headers: {
'x-auth-token': 'your_token_here'
},
data: {
// Your profile data
}
}).then(response => {
console.log(response.data); // Created or updated profile
}).catch(error => {
console.error('Error:', error);
});
// GET api/profile/user/:user_id
// Usage:
// 1. Add the following route in your express app
app.get('/api/profile/user/:user_id', checkObjectId('user_id'), async (req, res) => {
// Your route handler logic
});
// 2. Send a GET request
// e.g., using axios:
const axios = require('axios');
axios.get('http://localhost:5000/api/profile/user/12345')
.then(response => {
console.log(response.data); // Profile object
})
.catch(error => {
console.error('Error:', error);
});
// DELETE api/profile
// Usage:
// 1. Add the following route in your express app
app.delete('/api/profile', auth, async (req, res) => {
// Your route handler logic
});
// 2. Send a DELETE request
// e.g., using axios:
const axios = require('axios');
const token = 'your_auth_token'; // Replace with the user's auth token
axios.delete('http://localhost:5000/api/profile', {
headers: { 'x-auth-token': token },
})
.then(response => {
console.log(response.data); // { msg: 'User deleted' }
})
.catch(error => {
console.error('Error:', error);
});
// PUT api/profile/experience
// Usage:
// 1. Add the following route in your express app
app.put('/api/profile/experience', auth, check('title', 'Title is required').notEmpty(), ...);
// 2. Send a PUT request with experience data
// e.g., using axios:
const axios = require('axios');
const token = 'your_auth_token'; // Replace with the user's auth token
const experienceData = {
title: 'Software Engineer',
company: 'Awesome Company',
from: '2020-05-01'
};
axios.put('http://localhost:5000/api/profile/experience', experienceData, {
headers: { 'x-auth-token': token },
})
.then(response => {
console.log(response.data); // Updated profile
})
.catch(error => {
console.error('Error:', error);
});
// DELETE api/profile/experience/:exp_id
// Usage:
// 1. Add the following route in your express app
app.delete('/api/profile/experience/:exp_id', auth, async (req, res) => { ... });
// 2. Send a DELETE request with the experience ID to delete
// e.g., using axios:
const axios = require('axios');
const token = 'your_auth_token'; // Replace with the user's auth token
const experienceId = 'your_experience_id'; // Replace with the experience ID to delete
axios.delete(`http://localhost:5000/api/profile/experience/${experienceId}`, {
headers: { 'x-auth-token': token },
})
.then(response => {
console.log(response.data); // Updated profile
})
.catch(error => {
console.error('Error:', error);
});
// PUT api/profile/education
// Usage:
// 1. Add the following route in your express app
app.put('/api/profile/education', auth, check(...), async (req, res) => { ... });
// 2. Send a PUT request with the education data to add
// e.g., using axios:
const axios = require('axios');
const token = 'your_auth_token'; // Replace with the user's auth token
const educationData = {
school: 'Example University',
degree: 'Bachelor of Science',
fieldofstudy: 'Computer Science',
from: '2016-08-01',
to: '2020-05-01',
};
axios.put('http://localhost:5000/api/profile/education', educationData, {
headers: { 'x-auth-token': token },
})
.then(response => {
console.log(response.data); // Updated profile
})
.catch(error => {
console.error('Error:', error);
});
// DELETE api/profile/education/:edu_id
// Usage:
// 1. Add the following route in your express app
app.delete('/api/profile/education/:edu_id', auth, async (req, res) => { ... });
// 2. Send a DELETE request with the education ID to delete
// e.g., using axios:
const axios = require('axios');
const token = 'your_auth_token'; // Replace with the user's auth token
const educationId = '5d713995b721c3bb38c1f5d0'; // Replace with the education ID to delete
axios.delete(`http://localhost:5000/api/profile/education/${educationId}`, {
headers: { 'x-auth-token': token },
})
.then(response => {
console.log(response.data); // Updated profile
})
.catch(error => {
console.error('Error:', error);
});
Requires
- module:express
- module:middleware/auth
- module:express-validator
- module:models/Profile
- module:models/User