Part2:let ' S do "HELLO, world!"
Fire up your favorite text editor or IDE. I like Sublime Text a lot. Point it is at your Nodetest1 directory and open App.js. This is kind of the heart of your, the, and the app. Not a big surprise there. Here ' s a breakdown of what for you ' re going to:
Choose your favorite editor or integrated development environment. I like Sublime Text better. Click on your Nodetest1 directory to open the App.js file. This is the "heart" of your application.
var express = require (' Express ');
var routes = require ('./routes ');
var user = require ('./routes/user ');
var http = require (' http ');
var path = require (' path ');
1 The first part of the line before the inside as above, this creates a bunch the basic JavaScript variables and ties them to certain packages, dependencies, node functionality, and routes. This creates a series of JS variables that connect certain packages, dependencies, node, and routing.
var app = Express ();
2 This one ' s important. It instantiates Express and assigns our apps variable to it. The next section uses this variable to configure a bunch of Express stuff. This is very important to instantiate a Express object and assign it to the variable app. The following section can be used to configure some express content with this variable.
App.get ('/', routes.index);
App.get ('/users ', user.list);
3 Routing section
Http.createserver (APP). Listen (App.get (' Port '), function () {
console.log (' Express server listening on port ' + App.get (' Port ');
});
4 This section creates our HTTP service and launch it.
A: The 1th step: APP. JS in App.get () routing to join
App.get ('/helloworld ', routes.helloworld);
B: 2nd step: Open Routes/index. JS, add the following code at the end of the file:
Exports.helloworld = function (req, res) {
res.render (' HelloWorld ', {title: ' Hello, world! '});
C: 3rd Step: Views under the Create Helloworld.ejs file, the file entered the following content
Save the file, go to your command prompt, ctrl-c to kill app.js if it ' already running, and then type:
Save page, go to command window, CTRL + C kill app.js. (If the application is running) re-enter node App.js start.
D: 4th step: Http://localhost:3000/helloworld and enjoy the completely asinine text that gets displayed: