【问题标题】:Configuring local server with node js使用 node js 配置本地服务器
【发布时间】:2016-07-03 08:33:20
【问题描述】:

我的系统上安装了 Node JS,如何使用没有 wamp 或 xampp 的 node js 配置本地服务器,以便我可以在本地计算机上运行我的应用程序?有人可以帮助我一步一步的程序吗?

【问题讨论】:

  • 您获取或编写一个运行 Web 服务器的小型 node.js 应用程序(大约需要 10 行代码),然后您启动该应用程序。 node.js 本身不是 Web 服务器。一旦你有了这样的应用程序,它就可以运行作为 Web 服务器的应用程序。 Express 是一个 node.js 框架,它使 Web 服务器变得非常简单。这是 Express 中的一个简单示例 Web 服务器:expressjs.com/en/starter/hello-world.html

标签: node.js xampp localhost wamp localserver


【解决方案1】:

Node JS 让创建服务器变得非常简单。您需要做的就是编写一个文件:server.js。

var http = require("http");
var server = http.createServer(function(request, response) {
  response.end('Hello World!');
});

server.listen(8000);
console.log("Server is listening");

打开cmd写入

node server.js

转到您的浏览器并打开

localhost:8000

应该可以!

【讨论】:

    【解决方案2】:

    试试这个配置没有 wamp 的本地服务器 || Xampp

    var http = require('http'); // 1 - Import Node.js core module
    
    var server = http.createServer(function (req, res) {   // 2 - creating server
    
        //handle incomming requests here..
    
    });
    
    server.listen(5000); //3 - listen for any incoming requests
    
    console.log('Node.js web server at port 5000 is running..')
    

    【讨论】:

    • 请在代码前加四个空格来格式化您的代码,或者选择代码并点击编辑页面中的{}按钮。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-19
    • 2017-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    相关资源
    最近更新 更多