Develop a personal website using Express (i)

Source: Internet
Author: User
Tags readfile

Express, a fast, open, minimalist web development framework based on the node. JS platform.

Node's appearance, let JS have run on the server side of the possibility, based on this express, can quickly and easily build a server and personal website.

Installation

First install the Nodejs, the specific installation process is no longer described here, into the creation of the application directory, using the NPM init command to create a Package.json file, this command will require you to enter some information, such as personal information, application version number, etc., you can skip the use of the default, Except for one entry point, this parameter specifies the entry file for the current application, and you can use App.js or index.js some of your own set of entry files.

Next, install Express using the NPM Install Express command, and use the NPM install Express--save command If you need to add express to the dependency table.

Take a look at the app's directory:

  

Client is the main paging file, public is static file, App.js entry file

Hello World

You can start a simple server by adding the following code to the portal file App.js

  

var express = require (' Express '); var app = Express (); App.get ('/', function (req, res) {  res.send (' Hello world! ');}); var server = App.listen (5277, function () {  var host = Server.address (). Address;  var port = server.address (). Port;  Console.log (' Example app listening at http://%s:%s ', host, Port);});

Enter the command node app.js to run with the following results:

  

In the Web Access localhost:5277 you can see the returned Hello World.

Routing

Set up a route where the user accesses a specific URL to obtain the appropriate page.

A index.html home file is placed on the client, which is sent when the user accesses the home page address.

The homepage is as follows:

To read a file, you need to introduce node's file module FS:

var fs = require (' FS ');

Use the ReadFile function to read the file, ReadFile (Filename,[options],callback)

FileName: File name or path, you must

Options: Optional parameters, including read and write, no creation, already encoding encoded properties

Callback: callback function, default first parameter error, second data

App.get ('/', function (req, res) {    fs.readfile ('./client/index.html ', function (error,data) {    if (error)            { Error, error message        Res.writehead ($, {' Content-type ': ' Text/plain '});        Res.end (' 500-server Error ');    } else{            //success, occurrence file, and set type ' text/html '           res.writehead (200,{' content-type ': ' text/html '});           Res.end (data);});   });

Run, browser access localhost:5277, shown below

Need to access images, CSS and other static resources, can be built via Express express.static managed static files, set as follows:

Public is the static resource file address App.use (express.static (' public '));

You can use src= "images/img.png" to access your pictures directly from the home page.

  

Complete code: Https://github.com/Martian1/myweb

Develop a personal website using Express (i)

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.