Would you like to clone this notebook?

When you clone a notebook you are able to make changes without affecting the original notebook.

Cancel

My First Playground

node v8.17.0
version: 1.0.0
endpointsharetweet
This is a playground to test JavaScript. It runs a completely standard copy of Node.js on a virtual server created just for you. Every one of npm’s 300,000+ packages are pre-installed, so try it out:
console.log("sample code to save pending a application sent from user"); /**************** in mongoose.js ****************/ const mongoose = require('mongoose') // URI hard-coded in this demo, but should be exported from constants in production const mongoDB = 'mongodb+srv://demo:[email protected]/myFirstDatabase?retryWrites=true&w=majority' mongoose.connect(mongoDB, {useNewUrlParser: true, useUnifiedTopology: true}) const db = mongoose.connection db.on('error', console.error.bind(console, 'MongoDB connection error:')) db.once('open', () => { console.log("Connected to mongoDB server...") }) /**************** in models ****************/ const applicationSchema = mongoose.Schema({ email: { type: String, required: true, unique: true }, message: String, pending: { type: Boolean, default: true } }); const application = mongoose.model('Application', applicationSchema); /**************** in application.js ****************/ const jsonfile = require("jsonfile"); const inconsistentApplicationsPath = __dirname + "/inconsistentApplications.json"; async function receiveApplication (req, res) { try { console.log("get application from req") const newApplication = { "email": req.body.email, "message": req.body.message } try { console.log("save new application in db"); const application = await Application.create(newApplication); } catch (err) { console.log("save inconsistent applications in fs to be stored later in db"); jsonfile.writeFileSync(inconsistentApplicationsPath, newApplication); throw err; } /* due to RunKit API constraints, I can only send with status 200 :( */ return res.send({ success: true }); } catch (err) { console.error(err); return res.send({ success: false, error: `Internal Server Error ---> ${err}` }); } } /**************** RunKit endpoint config ****************/ var express = require("@runkit/runkit/express-endpoint/1.0.0"); var app = express(exports); app.get("*", (req, res) => {res.send({ success: true })}); app.post("/newApplication", (req, res) => { console.log("new"); receiveApplication(req, res); res.send({ success: true }); }); /**************** Unit testing ****************/ const axios = require("axios"); async function test () { const testResponse = await axios('https://untitled-z2vorvihfp7i.runkit.sh/newApplication', { email: `test${Math.random().toString(5)}@example.com`, message: "a sample application to be an admin" } ); console.log(await testResponse); } test();
Loading…

no comments

    sign in to comment