-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (66 loc) · 2.39 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Create Server Using NodeJs
const http = require('http');
const fs = require('fs');
const multer = require('multer');
// File Upload Settings
const storage = multer.diskStorage({
destination:(req,file,cb)=>{
cb(null,'uploads/');
},
filename:(req,file,cb)=>{
cb(null,Date.now() +"-"+ file.originalname);
},
});
let upload = multer({storage:storage}).single('img');
//..............................................................
let server = http.createServer(function(req,res){
if(req.url=='/'){
res.writeHead(200,{'Content-Type':'text/html'});
res.end('<h1> This is Home Page</h1>');
}else if(req.url=='/about'){
res.writeHead(200,{'Content-Type':'text/html'});
res.end('<h1> This is About Page</h1>');
}else if(req.url=='/contact'){
res.writeHead(200,{'Content-Type':'text/html'});
res.end('<h1> This is Contact Page</h1>');
// File Write
}else if(req.url=='/file-write'){
fs.writeFile('demo.txt','Hello World',function(error){
if(error){
res.writeHead(404,{'Content-Type':'text/html'});
res.end('<h1> Not found this text</h1>');
}else{
res.writeHead(200,{'Content-Type':'text/html'});
res.end('<h1> File write success...you can check Folder</h1>');
}
})
}
// File Upload
else if(req.url=='/register' && req.method==='GET'){
fs.readFile('upload.html', function(error, data){
if(error){
res.writeHead(404,{'Content-Type':'text/html'});
res.end('<h1> upload file not found</h1>');
}else if(data){
res.writeHead(200,{'Content-Type':'text/html'});
res.write(data);
res.end();
}
})
}else if(req.method ==='POST' && req.url === '/success'){
upload(req, res,(error)=>{
if(error){
res.writeHead(404,{'Content-Type':'text/html'});
res.end('<h1> upload file unsuccessful</h1>');
}else{
res.writeHead(200,{'Content-Type':'text/html'});
res.end('<h1> File upload success you can check folder</h1>');
}
});
}
// File up setting
});
// server create port
server.listen(5500, function(){
console.log('server is listening on port 5500');
});