Nodejs + Express + Ejs + MongoDB A very simple example of front and back end development 2__js

Source: Internet
Author: User
Tags mongodb sublime text

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:






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.