| सर्वर दुनिया | गोपनीयता नीति | सहायता / संपर्क करें |
3430 / 126206130
|
Node.js 18 : स्थापित करें2024/05/31 |
|
Node.js 18 स्थापित करें। |
|
| [1] | Node.js 18 स्थापित करें और परीक्षण स्क्रिप्ट चलाने का प्रयास करें। |
|
root@dlp:~#
root@dlp:~# apt -y install nodejs npm node -v v18.19.1 # परीक्षण स्क्रिप्ट बनाने के लिए सत्यापित करें
root@dlp:~# cat > nodejs_test.js <<'EOF'
var http = require('http');
var server = http.createServer(function(req, res) {
res.write("Hello, This is the Node.js Simple Web Server!\n");
res.end();
}).listen(8080);
EOF
root@dlp:~#
node nodejs_test.js & [1] 27114
root@dlp:~#
root@dlp:~# curl localhost:8080 Hello, This is the Node.js Simple Web Server! kill 27114 |
| [2] | एक नमूना चैट एप्लिकेशन बनाएं जिसे WebSocket कार्यान्वित करने के लिए कार्यान्वित किया गया है। |
|
ubuntu@dlp:~$
npm install fs socket.io express
ubuntu@dlp:~$
vi chat.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});
http.listen(1337, function(){
console.log('listening on *:1337');
});
ubuntu@dlp:~$
vi index.html
<!DOCTYPE html>
<html>
<head>
<title>WebSocket Chat</title>
</head>
<body>
<form action="">
<input id="sendmsg" autocomplete="off" /><button>Send</button>
</form>
<ul id="messages" style="list-style-type: decimal; font-size: 16px; font-family: Arial;"></ul>
<script src="/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery.min.js"></script>
<script>
var socket = io();
$('form').submit(function(){
socket.emit('chat message', $('#sendmsg').val());
$('#sendmsg').val('');
return false;
});
socket.on('chat message', function(msg){
$('#messages').append($('<li style="margin-bottom: 5px;">').text(msg));
});
</script>
</body>
</html>
node chat.js listening on *:1337 |
| [3] | यह सत्यापित करने के लिए नमूना प्रशंसा तक पहुंच कि यह सामान्य रूप से काम करता है। |
|
| Sponsored Link |
|
|