0% found this document useful (0 votes)
12 views6 pages

7 A

The document provides code snippets for implementing routing in a Node.js application, handling HTTP POST requests, and creating Mongoose schemas for MongoDB. It includes server setup for handling different routes, processing form data, and defining data models for 'Mage' and 'Post'. Additionally, it demonstrates error handling and the use of custom error types in Node.js.

Uploaded by

btechstencse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

7 A

The document provides code snippets for implementing routing in a Node.js application, handling HTTP POST requests, and creating Mongoose schemas for MongoDB. It includes server setup for handling different routes, processing form data, and defining data models for 'Mage' and 'Post'. Additionally, it demonstrates error handling and the use of custom error types in Node.js.

Uploaded by

btechstencse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

7a) noe js compiler onlineImplement routing for the AdventureTrails application by embedding the

necessary code in the routes/route.js file.

const http = require('http');

// Create a server object

http.createServer(function (req, res) {

// http header

res.writeHead(200, { 'Content-Type': 'text/html' });

const url = req.url;

if (url === '/about') {

res.write(' Welcome to about us page');

res.end();

else if (url === '/contact') {

res.write(' Welcome to contact us page');

res.end();

else {

res.write('Hello World!');

res.end();

}
}).listen(3000, function () {

// The server object listens on port 3000

console.log("server start at port 3000");

});

7b) HTTP POST requests work in Node


var http = require('http');
var qs = require('querystring');
var formOutput = '<html><body>'
+ '<h1>XYZ Repository Commit Monitor</h1>'
+ '<form method="post" action="inbound" enctype="application/x-www-form-
urlencoded"><fieldset>'
+ '<div><label for="UserName">User Name:</label><input type="text" id="UserName"
name="UserName" /></div>'
+ '<div><label for="Repository">Repository:</label><input type="text" id="Repository"
name="Repository" /></div>'
+ '<div><label for="Branch">Branch:</label><input type="text" id="Branch"
name="Branch" value="master" /></div>'
+ '<div><input id="ListCommits" type="submit" value="List Commits"
/></div></fieldset></form></body></html>';
var serverPort = 8124;
http.createServer(function (request, response) {
if(request.method === "GET") {
if (request.url === "/favicon.ico") {
response.writeHead(404, {'Content-Type': 'text/html'});
response.write('<!doctype
html><html><head><title>404</title></head><body>404: Resource Not
Found</body></html>');
response.end();
} else {
response.writeHead(200, {'Content-Type': 'text/html'});
response.end(formOutput);
}
} else if(request.method === "POST") {
if (request.url === "/inbound") {
var requestBody = '';
request.on('data', function(data) {
requestBody += data;
if(requestBody.length > 1e7) {
response.writeHead(413, 'Request Entity Too Large', {'Content-Type': 'text/html'});
response.end('<!doctype
html><html><head><title>413</title></head><body>413: Request Entity Too
Large</body></html>');
}
});
request.on('end', function() {
var formData = qs.parse(requestBody);
response.writeHead(200, {'Content-Type': 'text/html'});
response.write('<!doctype
html><html><head><title>response</title></head><body>');
response.write('Thanks for the data!<br />User Name: '+formData.UserName);
response.write('<br />Repository Name: '+formData.Repository);
response.write('<br />Branch: '+formData.Branch);
response.end('</body></html>');
});
} else {
response.writeHead(404, 'Resource Not Found', {'Content-Type': 'text/html'});
response.end('<!doctype html><html><head><title>404</title></head><body>404:
Resource Not Found</body></html>');
}
} else {
response.writeHead(405, 'Method Not Supported', {'Content-Type': 'text/html'});
return response.end('<!doctype
html><html><head><title>405</title></head><body>405: Method Not
Supported</body></html>');
}
}).listen(serverPort);
console.log('Server running at localhost:'+serverPort);

Custom error types in Node.js


class MyError extends Error {
constructor(message) {
super(message)
this.name = 'MyError'
Error.captureStackTrace(this, MyError)
}
}

7c.Write a Mongoose schema to connect with MongoDB.

const mongoose = require('mongoose');

mongoose.connect("mongodb://localhost:27017/magesDB");

const mageSchema = new mongoose.Schema({

name: {
type: String,

require: true

},

power_type: {

type: String,

require: true

},

mana_power: Number,

health: Number,

gold: Number

})

7d. write a program to the schema into a model object in node js example

const Mage = new mongoose.model("Mage", mageSchema)

const mage_1 = new Mage({

name: "Takashi",

power_type: 'Element',

mana_power: 200,

health: 1000,

gold: 10000

});

mage_1.save();
const mage_2 = new Mage({

name: "Steve",

power_type: 'Physical',

mana_power: "500",

health: "5000",

gold: "50"

})

mage_2.save();

const likeSchema = mongoose.Schema({

type: [String],

default: [] },

});

const postSchema = mongoose.Schema({

title: String,

message: String,

name: String,

creator: String,

tags: [String],

selectedFile: String,

likes: {

type: mongoose.Schema.Types.String,

ref: 'Like',
},

createdAt: {

type: Date,

default: new Date(),

},

})

const Post = mongoose.model("Post", postSchema);

const posts = await Post.findById({creator_id}).populate("likes");

//Below code will print the data of the first element of the type array of relevant likes document.

console.log(posts.likes.type[0]);

You might also like