在Node.js服务器中提供HTML文件 [英] Serve html file in nodejs server

查看:63
本文介绍了在Node.js服务器中提供HTML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直很好,直到尝试将代码分为路由,控制器等为止.现在尝试加载html文件时出现错误.当我转到链接

服务器启动无错误.而且我认为我已经正确给出了路径.但是我得到了错误.谁能帮我这个 ?谢谢.

这就是我将脚本链接到html文件的方式

 < script src ="/script/app.js"></script> 

解决方案

已经两个月了,您解决了这个问题吗?

如果没有,请尝试输入该代码:

  app.use(express.static('app')); 

您赋予 static 函数的路径是相对于您运行节点进程的目录的.

在您的情况下,您使用/strong.server.js 文件(在根目录中)启动服务器,因此给 static 函数的路径是相对于此位置.

I've been doing fine until I try to separate my code into routes, controllers and etc. Now I'm getting an error when I try to load the html file. When I go to the link http://localhost:3000/ I'm getting this error Error: ENOENT: no such file or directory, stat '/views/index.html'

This is my routes.js code

module.exports = function (app) {
    var userController = require('../controllers/userController');

    // app.use(require('express').static('../app/views/index.html'));

    app.get('/', userController.renderHomePage);

    app.post('/find', userController.getUser);
    app.get('/get', userController.getUsers);
    app.post('/add', userController.addUser);
}

And here's my userController.js file

var mongoose = require('mongoose');
var User = require('../models/user');

var express = require('express');

var app = express();
app.use(express.static('../app'));

exports.renderHomePage = function (req, res) {
    res.sendFile('/views/index.html');
}

exports.addUser = function(req,res){
    console.log(req.body);

    var newUser = new User({
        name : req.body.name,
        username : req.body.username,
        password : req.body.password
    });

    newUser.save(function(err){
        if(err){
            console.log(err);
        }
        else{
            console.log("User Saved successfully");
        }
    });

    res.send(req.body);
};

exports.getUsers = function (req, res) {
    // body...
    User.find({}, function(error, users){
        if(error){
            console.log(error);
        }
        else{
            res.send(users);
        }
    })
};

exports.getUser = function (req, res) {
    // body...
    console.log(req.body);
    var data = req.body.username;

    User.find({username : data}, function(err, user){
        if(err){
            throw err
        }
        else{
            console.log(user);
            res.send(user);
        }
    } );
};

Here's my server.js

var express = require('express');
// var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var PORT = process.env.PORT || 3000;

var app = express();

var routes = require('./api/routes/routes');
routes(app);

var database = require('./config/database');

app.use(bodyParser.json());

app.listen(PORT, function(){
    console.log("Server is running on port "+PORT);
})

And here's my folder structure.

Server starting without an error. And I thought I've given the paths correctly. But I'm getting the error. Can anyone help me with this ? Thanks.

EDIT : This is how I've linked my script to the html file

<script src="/script/app.js"></script>

解决方案

It's been two months, did you solve the problem ?

If not did you try that code :

app.use(express.static('app'));

The path you give to the static function is relative to the directory where you run your node process.

In your case, you start your server with/from the server.js file (at the root directory), so the path you give to the static function is relative to this location.

这篇关于在Node.js服务器中提供HTML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆