Skip to content

Instantly share code, notes, and snippets.

@shyampurk
Created February 11, 2022 06:52
Show Gist options
  • Save shyampurk/dffedb478b9305eb0600abbe7bf45932 to your computer and use it in GitHub Desktop.
Save shyampurk/dffedb478b9305eb0600abbe7bf45932 to your computer and use it in GitHub Desktop.
LightRun-Nodejs-HTTP-Sample
require('lightrun').start({
lightrunSecret: '<YOUR_LIGHTRUN_SECRET>',
company: '<YOUR_COMPANY_NAME>',
});
const express = require('express');
const req = require('express/lib/request');
const app = express();
let bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
let taskData = []
function main(){
app.post('/task',function(req,res){
createTask(req.body,res)
})
app.get('/task/:id',function(req,res){
getTask(req.params.id,res)
})
}
app.listen(80,()=>{
main()
console.log('server listening to port 80')
})
function createTask(body,res){
let id = taskData.length + 1
let newTask = {
taskId: id,
descr : body.descr
}
taskData.push(newTask)
res.setHeader('content-type', 'Application/json');
res.statusCode = 200;
res.json({ task: newTask });
}
function getTask(id,res){
let readTask = null
taskData.every(function(element, index){
if(element.taskId == id) {
readTask = element
return false
} else {
return true
}
})
if(readTask){
res.setHeader('content-type', 'Application/json');
res.statusCode = 200;
res.json({ task: readTask });
} else {
res.status(404).send('Task not found!!')
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment