In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Please follow my small site: http://oideas.herokuapp.com/1. README
Why do you have to tell yourself? Well, let's talk about something first. first of all, I'm not a big god. I just got out of being single (graduated in 2013 and got off on February 8, 2014). The research on nodejs adds up to only two weeks. I used to be a java guy (and now I'm just doing it casually). Out of curiosity about node, I spent half a day studying it, and then. There it is, and... So why not Oideas's demo-related blog, but OMinds's blog? For, blue is better than blue. It's just a step forward.
In addition, the study of this site has a lot of reference to @ nswbmw's blog, and students who want to build blog can see TA's wiki
In addition, if blog doesn't write well, please feel free to spray it.
two。 Environment building
The environment of this site
Node.js: 0.10.25
Express: 3.2.6
Mongodb: 2.4.9
Development tools
Nodeeclipse
Environment installation (if the environment has been built, please skip the step) the following are all on the windows platform
To install nodejs, go to the Node official website to download the corresponding msi (mine is 64bit, download is Windows Installer (.msi) 64-bit), click install, and click next. Verify that the environment has been successfully installed. Type node-v under cmd to display the relevant version.
To install express, under cmd, type: npm install-g express and wait for download.
Install mongodb, download it from mongo's official website, and decompress the corresponding package.
Install the node development plug-in under eclipse and check out the latest update site. Currently: http://dl.bintray.com/nodeclipse/nodeclipse/0.10.0/, then open eclipse help- > install new software and fill in the update site address. You don't have to install everything. Generally, core should be installed, and other tools should choose to install it.
3. Newly built project
Open eclipse and create a new Node.js Express Project. Select ejs with the name OMinds,Template Engine (other templates are studied by yourself). Now that project has been ok, switch to your project directory and enter node app.js under cmd, as shown below:
Open the browser: http://localhost:3000/ can see the following figure:
(note: another way to build a project is to create a new project name directly, and cmd changes to the same level directory of the project. For example, if the project is d:\ OMinds, then cmd can be switched to d disk, and then type: cd OMinds: npm install after express-e OMinds installation is successful.
4. Engineering catalogue
(this part mainly draws lessons from the explanation of the great god nswbmw)
App.js: startup file, or entry file
Package.json: stores project information and module dependencies. When adding dependent modules to dependencies, running npm install,npm will check the package.json in the current directory and automatically install all specified modules
Node_modules: store the modules installed in package.json. When you add dependent modules to package.json and install them, store them in this folder.
Public: store p_w_picpath, css, js and other files
Routes: store routing files
Views: store view files or template files
Open app.js, as shown below:
App.set ('port', process.env.PORT | | 3000): set the port to process.env.PORT or 3000.
App.set ('views', _ dirname +' / views'): set the views folder to the directory where the view files are stored, that is, the place where the template files are stored, and dirname is the global variable that stores the directory where the currently executing script is located.
App.set ('view engine',' ejs'): sets the view template engine to ejs.
App.use (express.favicon ()): connect built-in middleware, using the default favicon icon, if you want to use your own icon, you need to change to app.use (express.favicon (dirname +'/ public/p_w_picpaths/favicon.ico')); here we put the custom favicon.ico under the / public/p_w_picpaths folder.
App.use (express.logger ('dev')): connect built-in middleware, used in a development environment, displays simple logs on the terminal, such as accessing localhost:3000 after starting app.js, and the terminal will output:
Express server listening on port 3000GET / 20021ms-206bGET / stylesheets/style.css 3044ms >
If you remove this line of code, no matter how much you refresh the page, the terminal has only one line of Express server listening on port 3000.
App.use (express.bodyParser ()): connect built-in middleware for parsing the request body, supporting application/json, application/x-www-form-urlencoded, and multipart/form-data.
App.use (express.methodOverride ()): connect's built-in middleware that assists in processing POST requests and disguises PUT, DELETE, and other HTTP methods.
App.use (app.router): the rule that invokes route resolution.
App.use (express.static (path.join (_ _ dirname, 'public'): connect built-in middleware that sets the public folder in the root directory to the directory where static files such as p_w_picpath, css and js are stored.
If ('development' = = app.get (' env')) {app.use (express.errorHandler ());}
Error handling in the development environment, output error information.
App.get ('/', routes.index): routing controller. If the user visits / (home page), it is handled by routes.index. The routes/index.js content is as follows:
Exports.index = function (req, res) {res.render ('index', {title:' Express'});}
The index function interface is exported through exports.index. App.get ('/', routes.index) is equivalent to:
App.get ('/', function (req, res) {res.render ('index', {title:' Express'});})
The above content introduction is from nswbmw, why did I take it directly, because what TA said is very good, and this part is still very useful. So now, delete from the app.js file:
App.get ('/', routes.index); app.get ('/ users', user.list)
Add: routes (app)
And modify it in index.js as follows:
Module.exports = function (app) {app.get ('/', function (req, res) {res.render ('index', {title:' Express'});}
Start the service under cmd again, open the browser, and you will find that the effect is the same.
Why delete the above two lines of code, app.get ('/ users', user.list); / / routing control every additional routing control will be followed by an app.get () or app.post (). When there are many routes, there will be a lot of code in app.js, which is not easy to maintain and modify. So put it another way, write routing control in index.js.
The above is the main content of this chapter, and the next chapter starts to build our OMinds.
The source code has been uploaded to git. Click here to download
Ideas-ominds communication group: 158325682, if you want to do it together, or if you have anything you don't understand, you can contact me.
For the first time, blog didn't write very well, but it will be improved later.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.