[英]Would love some help determining where to start with the Node.JS app file provided with BlueMix
我一直在閱讀 JS 教程,並且對語法和方法有一個很好的了解,現在我正在尋找如何使用 Node.JS 來構建帶有 BlueMix 的應用程序。 我對 Java 非常流利,但我對網絡編程完全陌生,所以我對如何開始感到非常迷茫。 請原諒我目前絕對是初學者的狀態,但我真的被困住了。 這是他們給你的文件。
/*eslint-env node*/
//--------------------------------------------------------------------------
// node.js starter application for Bluemix
//--------------------------------------------------------------------------
// This application uses express as its web server
// for more info, see: http://expressjs.com
var express = require('express');
// cfenv provides access to your Cloud Foundry environment
// for more info, see: https://www.npmjs.com/package/cfenv
var cfenv = require('cfenv');
// create a new express server
var app = express();
// serve the files out of ./public as our main files
app.use(express.static(__dirname + '/public'));
// get the app environment from Cloud Foundry
var appEnv = cfenv.getAppEnv();
// start server on the specified port and binding host
app.listen(appEnv.port, '0.0.0.0', function() {
// print a message when the server starts listening
console.log("server starting on " + appEnv.url);
});
為了澄清起見,我了解了代碼的一般用途 - 它的注釋很好,但我只是不知道如何在 BlueMix 平台上開始 // 測試。 我嘗試做諸如添加打印語句之類的事情,但沒有真正改變。
對我正在嘗試做的事情有一點了解:只需創建一個網頁,用戶可以在其中輸入一個字符串,然后我可以發布一個字符串作為響應。 我只是想學習 BlueMix,所以我需要在這個平台上和 Node JS 中學習。
我建議您查看本教程: IBM Bluemix DevOps Services - Develop and deploy a Node.js app 。 開始使用 DevOps Services(閱讀您的評論,似乎您正在使用它從 Web IDE 部署到 Bluemix)在 Bluemix 上獲得“Hello World”Node.js Web 應用程序非常有用。 它還展示了如何應用一些更改並直接在 Bluemix 上重新推送它們。
只是為了回答您的問題,假設您已經部署了入門應用程序(如您的示例所示):
app.get('/printhello', function (req, res) { res.send('Hello, World!'); });
最后,我邀請您看一下Bluemix - Node.js SDK文檔,它非常簡單明了。
我希望這能給你一些起點。
查看https://github.com/IBM-Bluemix/bluemix-hello-node 。 它為 node 提供了一個很好的起點。 您需要編寫一些代碼,Node 才能在 Bluemix 上工作。 最大的是綁定到正確的端口,端口是通過process.env.PORT
給出的。 我在下面粘貼了一個超級簡單的 Node.app(其余代碼位於上面的 GitHub 鏈接)。
var express = require("express"),
app = express();
var port = process.env.PORT || 8080;
app.use(express.static(__dirname + '/public'));
app.get("/hello", function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"})
response.end("Hello World!\n");
});
app.listen(port);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.