Jin's Blog

[Node.js] prints received text back on URL with body-parser

July 26, 2017 | 0 Minute Read

This post is based on my personal experiences.

The following codes show you how to basic communication between Front-end and Back-end with Node.js. to receive or send received text back to URL. we must install a module called “body-parser”

  1. npm install body-parser –save
var express = require('express');
var app = express();
var bodyParser = require('body-parser');

app.listen(3000, function() { // when connection with port 3000 is linked then the hello message would be printed.
	console.log("Hello back-end");
});

app.use(express.static('public'))
app.use(bodyParser.json()) //to receive json format text
app.use(bodyParser.urlencoded({extended:true})) //to receive any format excluding json

app.get('/', function(req, res) { //for default page
	console.log('test');
	res.sendFile(__dirname + '/public/main.html')
});

app.get('/main', function(req,res) { // when connection with URL "../main" is linked then, the main.html page would be displayed with the console message.
	console.log("you are in main domain");
	res.sendFile(__dirname + '/public/main.html')
});

app.post('/email_post', function(req, res){ // lastly, this function works similarly as previous method. But, it prints received text on webpage.
	//to receive "post" we must declear body-parser;
	console.log("yes you are entered");
	console.log(req.body.email);
	res.send("<h1>welcome " + req.body.email + " </h1>")

});