├── server.js ├── package.json ├── README.md ├── controller.js ├── LICENSE └── service.js /server.js: -------------------------------------------------------------------------------- 1 | 2 | const hostname = '127.0.0.1'; 3 | const port = 3000; 4 | 5 | const server = require('./controller.js'); 6 | 7 | server.listen(port, hostname, () => { 8 | console.log(`Server running at http://${hostname}:${port}/`); 9 | }); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "simple-rest-apis-nodejs-without-express", 3 | "version": "1.0.0", 4 | "description": "simple rest apis in nodejs without express", 5 | "main": "server.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/aditya-sridhar/simple-rest-apis-nodejs-without-frameworks.git" 12 | }, 13 | "keywords": [ 14 | "rest", 15 | "apis", 16 | "node", 17 | "express" 18 | ], 19 | "author": "Aditya Sridhar", 20 | "license": "MIT", 21 | "bugs": { 22 | "url": "https://github.com/aditya-sridhar/simple-rest-apis-nodejs-without-frameworks/issues" 23 | }, 24 | "homepage": "https://github.com/aditya-sridhar/simple-rest-apis-nodejs-without-frameworks#readme" 25 | } 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Building REST apis with Nodejs without using any framework or external library 2 | 3 | This Repo is part of a blog. 4 | 5 | The link to the blog is [https://adityasridhar.com/posts/how-to-use-nodejs-without-frameworks-and-external-libraries](https://adityasridhar.com/posts/how-to-use-nodejs-without-frameworks-and-external-libraries) 6 | 7 | ## Pre-requisite 8 | 9 | Ensure You have NodeJS installed in your system. 10 | Refer to [https://nodejs.org](https://nodejs.org) to install NodeJS 11 | 12 | ## Cloning and Running the Code 13 | 14 | This code can be cloned to your local using the command 15 | 16 | ```bash 17 | git clone https://github.com/aditya-sridhar/simple-rest-apis-nodejs-without-frameworks.git 18 | ``` 19 | 20 | ## Running the code 21 | 22 | `server.js` is the starting point of the code 23 | 24 | The application can be started using the command 25 | 26 | ```bash 27 | node server.js 28 | ``` 29 | -------------------------------------------------------------------------------- /controller.js: -------------------------------------------------------------------------------- 1 | const http = require('http'); 2 | const url = require('url'); 3 | 4 | module.exports = http.createServer((req, res) => { 5 | 6 | var service = require('./service.js'); 7 | const reqUrl = url.parse(req.url, true); 8 | 9 | // GET Endpoint 10 | if (reqUrl.pathname == '/sample' && req.method === 'GET') { 11 | console.log('Request Type:' + 12 | req.method + ' Endpoint: ' + 13 | reqUrl.pathname); 14 | 15 | service.sampleRequest(req, res); 16 | 17 | // POST Endpoint 18 | } else if (reqUrl.pathname == '/test' && req.method === 'POST') { 19 | console.log('Request Type:' + 20 | req.method + ' Endpoint: ' + 21 | reqUrl.pathname); 22 | 23 | service.testRequest(req, res); 24 | 25 | } else { 26 | console.log('Request Type:' + 27 | req.method + ' Invalid Endpoint: ' + 28 | reqUrl.pathname); 29 | 30 | service.invalidRequest(req, res); 31 | 32 | } 33 | }); -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Aditya Sridhar 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /service.js: -------------------------------------------------------------------------------- 1 | const url = require('url'); 2 | 3 | exports.sampleRequest = function (req, res) { 4 | const reqUrl = url.parse(req.url, true); 5 | var name = 'World'; 6 | if (reqUrl.query.name) { 7 | name = reqUrl.query.name 8 | } 9 | 10 | var response = { 11 | "text": "Hello " + name 12 | }; 13 | 14 | res.statusCode = 200; 15 | res.setHeader('Content-Type', 'application/json'); 16 | res.end(JSON.stringify(response)); 17 | }; 18 | 19 | exports.testRequest = function (req, res) { 20 | body = ''; 21 | 22 | req.on('data', function (chunk) { 23 | body += chunk; 24 | }); 25 | 26 | req.on('end', function () { 27 | 28 | postBody = JSON.parse(body); 29 | 30 | var response = { 31 | "text": "Post Request Value is " + postBody.value 32 | }; 33 | 34 | res.statusCode = 200; 35 | res.setHeader('Content-Type', 'application/json'); 36 | res.end(JSON.stringify(response)); 37 | }); 38 | }; 39 | 40 | exports.invalidRequest = function (req, res) { 41 | res.statusCode = 404; 42 | res.setHeader('Content-Type', 'text/plain'); 43 | res.end('Invalid Request'); 44 | }; --------------------------------------------------------------------------------