How to use 'how to start json server' in JavaScript

Every line of 'how to start json server' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
93function startServer(server, port = 3000) {
94 server.use(bodyParser.json())
95
96 // The new /save-subscription endpoint
97 server.post('/save-subscription', async function(req, res){
98 const subscription = req.body
99 await saveToDatabase(subscription) //Method to save the subscription to Database
100 res.json({ message: 'success' })
101 })
102
103 server.get('/send-notification', function(req, res) {
104 const subscription = dummyDb.subscription //get subscription from your databse here.
105 if (subscription) {
106 const message = 'Hello World'
107 sendNotification(subscription, message)
108 res.json({ message: 'message sent' })
109 } else {
110 res.json( {error: 'nothing'})
111 }
112 })
113
114 server.start({
115 port: process.env.PORT || port,
116 endpoint: '/graphql',
117 playground: '/playground',
118 subscriptions: {
119 path: '/subscriptions',
120 onConnect,
121 onDisconnect,
122 }
123 }, (options) => {
124 console.log(`Server is running on http://localhost:${port}`)
125 const maxListenersExceededWarning = require('max-listeners-exceeded-warning');
126 maxListenersExceededWarning();
127 }
128 )
129}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code


Related snippets