0% found this document useful (0 votes)
6 views

nodejs2

it is about node js

Uploaded by

khanuneza551
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

nodejs2

it is about node js

Uploaded by

khanuneza551
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Example Node.

js Application
const http = require('http')

const hostname = '127.0.0.1'


const port = 3000

const server = http.createServer((req, res) => {


res.statusCode = 200
res.setHeader('Content-Type', 'text/plain')
res.end('Hello World\n')
})

server.listen(port, hostname, () => {


console.log(`Server running at http://$
{hostname}:${port}/`)
})

A node.js web application contains the following three parts:

1. Import required modules: The "require" directive is used to load a


Node.js module.
2. Create server: You have to establish a server which will listen to
client's request similar to Apache HTTP Server.
3. Read request and return response: Server created in the second
step will read HTTP request made by client which can be a browser or
console and return the response.
4. Node.js Path Methods
5. Let's see the list of methods used in path module:

Method Description

path.normalize(p) It is used to normalize a string path, taking care of '..' and '.' pa

path.join([path1][, It is used to join all arguments together and normalize the resul
path2][, ...])

path.resolve([from ...], It is used to resolve an absolute path.


to)

path.isabsolute(path) It determines whether path is an absolute path. an absolute


same location, regardless of the working directory.

path.relative(from, to) It is used to solve the relative path from "from" to "to".

path.dirname(p) It return the directory name of a path. It is similar to the unix di

path.basename(p[, It returns the last portion of a path. It is similar to the Unix base
ext])

path.extname(p) It returns the extension of the path, from the last '.' to end o
path. if there is no '.' in the last portion of the path or the first c
an empty string.

path.parse(pathstring) It returns an object from a path string.

path.format(pathobject It returns a path string from an object, the opposite of path.pars


)

Node.js provides the facility to get process information


such as process id, architecture, platform, version,
release, uptime, upu usage etc. It can also be used to kill
process, set uid, set groups, unmask etc.

The process is a global object, an instance of


EventEmitter, can be accessed from anywhere.

Formidable module is used for parsing form data, especially file


uploads. It is easy to use and integrate into your project for
handling incoming form data and file uploads.
Installation of formidable module:
1. using this command.
npm install formidable
2. After installing formidable module, you can check your yargs
version in command prompt using the command.
npm version formidable
3. After that, you can just create a folder and add a file for
example index.js, To run this file you need to run the following
command.
node index.js
const fs = require('fs');
const path = require('path')
const formidable = require('formidable');

const app = express();

app.post('/api/upload', (req, res, next) => {

const form = new formidable.IncomingForm();


form.parse(req, function(err, fields, files){

var oldPath = files.profilePic.path;


var newPath = path.join(__dirname, 'uploads')
+ '/'+files.profilePic.name
var rawData = fs.readFileSync(oldPath)

fs.writeFile(newPath, rawData, function(err){


if(err) console.log(err)
return res.send("Successfully uploaded")
})
})
});

app.listen(3000, function(err){
if(err) console.log(err)
console.log('Server listening on Port 3000');
});

You might also like