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)
);
Hi @dhyan
I believe it should work on windows as well. Are you following the tutorial from the below docs?
https://v1docs.moralis.io/moralis-dapp/getting-started/self-hosting-moralis-server
Also try to get the latest repo clone from the github.