I have de following code in node js:
var app,fs, express;
fs = require('fs');
app = require('http').createServer(function(req, res) {
var file;
file = req.url === '/' ? '/index.html' : req.url;
console.log(req.method + " " + file);
return fs.readFile("./views" + file, function(err, data) {
if (err != null) {
res.write(404);
return res.end("<h1>HTTP 404 - Not Found</h1>");
}
res.writeHead(200);
return res.end(data);
});
});
app.listen(3000, function() {
return console.log("running...);
});
i need insert expressjs in my application for work with express js for example :
app = express();
express = require("express");
app.use(express.bodyParser());
app.use(app.router);
app.get("/form", function (req,res){
res.sendfile("./form.html");
});
http.createServer(app).listen(3000);
how join the two code in one
By joining 2 code in one , i guess you want to use the middleware you described in 1st file
var app = express();
var express = require("express");
app.use(express.bodyParser());
app.use(app.router);
app.get("/form", function (req,res){
res.sendfile("./form.html");
});
app.use(function(req, res) {
var file;
file = req.url === '/' ? '/index.html' : req.url;
console.log(req.method + " " + file);
return fs.readFile("./views" + file, function(err, data) {
if (err != null) {
res.write(404);
return res.end("<h1>HTTP 404 - Not Found</h1>");
}
res.writeHead(200);
return res.end(data);
})
http.createServer(app).listen(3000);
// Create app with Express
let app = express();
// Create a Node server for Express app
let server = http.createServer(app);
// Parse application/json
app.use(bodyParser.json());
// Parse application/vnd.api+json as json
app.use(bodyParser.json({ type: 'application/vnd.api+json' }));
// Parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/form", function (req,res){
res.sendfile("./form.html");
});
app.use(function(req, res) {
var file;
file = req.url === '/' ? '/index.html' : req.url;
console.log(req.method + " " + file);
return fs.readFile("./views" + file, function(err, data) {
if (err != null) {
res.send(404);
}
res.json(data);
})
server.listen(3000);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.