const express = require('express');
const bodyParser = require('body-parser');
const { ParseServer } = require('parse-server');
const ParseDashboard = require('parse-dashboard');
const app = express();
// Parse Server initialization
async function startParseServer() {
const parseServer = new ParseServer({
databaseURI: 'mongodb://127.0.0.1:27017/test', // MongoDB URI
cloud: './cloud/main.js',
fileKey: 'optionalFileKey',
serverURL: 'http://192.168.1.5:1337/parse', // Server URL
appId: 'myAppId',
masterKey: 'myMasterKey', // Keep this key secret!
encodeParseObjectInCloudFunction: false,
});
// Mount Parse Server at '/parse' URL prefix
app.use('/parse', parseServer.app);
// Configure Parse Dashboard (optional)
const dashboard = new ParseDashboard({
apps: [
{
serverURL: 'http://192.168.1.5:1337/parse',
appId: 'myAppId',
masterKey: 'myMasterKey',
appName: "MyApp"
},
],
users: [
{
user: 'admin',
pass: 'password', // Keep this secure!
},
],
// Allow insecure HTTP (for development only)
allowInsecureHTTP: false,
});
// Mount Parse Dashboard at '/dashboard' URL prefix (optional)
app.use('/dashboard', dashboard);
// Body-parser middleware to parse incoming request bodies
app.use(bodyParser.json()); // Parse JSON-encoded bodies
// Define a GET endpoint to retrieve data
app.get('/api/data', async (req, res) => {
try {
// Example query to retrieve data from a Parse class
const query = new Parse.Query('MyClass');
const results = await query.find({ useMasterKey: true });
res.json(results);
} catch (error) {
console.error('Error fetching data:', error);
res.status(500).json({ error: 'Error fetching data' });
}
});
// Define a POST endpoint to add data with just a 'name' field
app.post('/api/data', async (req, res) => {
try {
const { name } = req.body;
if (!name) {
return res.status(400).json({ error: 'Name is required' });
}
// Create a new object in a Parse class
const MyClass = Parse.Object.extend('MyClass');
const myObject = new MyClass();
myObject.set('name', name);
// Save the object to the database
await myObject.save(null, { useMasterKey: true });
res.json({ message: 'Data saved successfully', objectId: myObject.id });
} catch (error) {
console.error('Error saving data:', error);
res.status(500).json({ error: 'Error saving data' });
}
});
// Start the server
const port = 1337;
app.listen(port, function () {
console.log(`parse-server-example running on port ${port}.`);
});
}
// Call the async function to start Parse Server
startParseServer().catch((err) =>
console.error('Error starting Parse Server:', err)
);
did you get it to work?
Have you tried using localhost instead of 192.168.1.5?
I have tried but can’t find any proper solution.
Yes, I used “localhost” instead of “192.168.1.5”, but the same issue is still there.
so if your parse-server is running properly, the problem might be your settings to connect to parse-server.
check your app settings and make sure you have it the same as your initialization and there’s no typo.
mainly check
appId: 'myAppId',
masterKey: 'myMasterKey',
are the same with whatever you’re using to connect to parse-server. which i think is react-admin-demo
app setting and initialization config both are same but still issue is same
when you open 192.168.1.5:1337/parse in your browser, do you see
{"error":"unauthorized"}
?
Yes when I open 192.168.1.5:1337/parse in browser I get {“error”:“unauthorized”}
@dhyan Hi,
You still don’t start the parse-server, but you are trying to connect the un-started server.
Add this line:
await parseServer.start()
before this :
// Mount Parse Server at '/parse' URL prefix
app.use('/parse', parseServer.app);
@dhyan Did you change according to this?
@rgunindi I have changed what you said and the code is working fine.
I wholeheartedly thank you for your help.
You are welcome Please select my answer as the solution.
Good Work